jQuery Syntax

Jun 5, 2011 at 3:26 PM

Hi folks. This should be a 2 second answer for most.

I have this script below that (yes, beleive it or not) has taken me all weekend to write with some help from a post by Holmes. Go ahead laugh, I don't blame you, I don't claim to be a programmer, it just logically makes sense to me, plus it works perfectly. My question is simply; is it proper jQuery'ese' or is it an old, long and drawn out way of using javascipt? Your answers are very much appreciated as I'm trying to learn the language. If not that's ok, I'm sure most have higher level code to deal with anyway.

function PreSaveAction() {

var dateFre= $(":input[title='Date de fin']").val();
var alertFre= "Veuillez choisir une date puis antérieure ou égale à aujourd'hui"
var dateTitleFre= 'Date de fin'

var dateEng= $(":input[title='Date Completed']").val();
var alertEng= "Please choose a date equal to or earlier then today"
var dateTitleEng= 'Date Completed'

var date1    //we will define this once we find out what language the field "Date Comnpleted" is displayed in
var alertText //we will define this once we find out what language the field "Date Comnpleted" is displayed in
var dateTitle //this is used at the end of the script

//even though we know the field's name is "Date Completed", it might not be displaying in english

var dateIDFre= $(":input[title='Date de fin']").attr("id");  //if the form is in French this will return a cryptic ID, not the ordinary 32 character guid
                // otherwise it would return: undefined
var dateIDEng= $(":input[title='Date Completed']").attr("id"); //if the form is in Englidh this will return a cryptic ID, not the ordinary 32 character guid
                // otherwise it would return: undefined

//ok now we find out what language the form is displayed in.

 if (dateIDFre) {
  //alert ("french");
  date1 = dateFre;   //the value of the date when viewed in french
  dateTitle = dateTitleFre; //the field's nname/label
  alertText = alertFre;

 }
 else {
  //alert ("english");
  date1 = dateEng;
  dateTitle = dateTitleEng;
  alertText = alertEng;
 }

var arrDate1 = date1.split("/");
var useDate1 = new Date(arrDate1[2], arrDate1[1]-1, arrDate1[0]);

if(useDate1 > new Date()) {
alert (alertText);

// if this were only in English we could use: var fieldConc = $(":input[title='Date Completed']");
//but I'm using the other variable dateTitle in place of 'Date Completed'

var fieldConc = $(":input[title='dateTitle']");
fieldConc.focus();
return false;
}
return true;
}

Thanks

Carl

Coordinator
Jun 6, 2011 at 4:17 AM

Carl:

First off, if it works, that's winning the main battle!

Be sure to always end your lines with a semi-colon. Sometimes it won't matter, and other times you'll end up tearing your hair out.

This is definitely more JavaScript-like than jQuery-like, but that's not necessarily bad. Because you can chain jQuery functions, you can pack the code a little bit more tightly. I'm not going to try to rewrite any bits, but chaining would definitely make it tighter and perhaps a little easier to follow.

M.

Jun 6, 2011 at 2:40 PM

Thanks for the commentary!! Yes, I keep forgetting the semi-colons and "{" brackets. I was more used to VB.

Cheers