Stampa
Categoria: Javascript
Visite: 5357
Stella inattivaStella inattivaStella inattivaStella inattivaStella inattiva
 

Some useful Javascript Snippets 

 


Test Var Undefined and default value for argument

if (typeof variable ==='undefined') {
   // your code
}
//  the following function has the last 2 arguments nont mandatory
function bymanHeightEqual(pElementMain, pElementCompare, pHeight, pHeightPlus ) {

// default value for arguments not mandatory
pHeight=pHeight || "higher";
pHeightPlus=pHeightPlus || 0 ;

 


Search in the URL

// Search in the url
if(location.href.indexOf('/en/')>-1){
  $("body").addClass("enClass");
}

 


Days in a month

function getDaysInMonth(m, y){return/8|3|5|10/.test(--m)?30:m==1?(!(y%4)&&y%100)||!(y%400)?29:28:31;}

 


 

URL like #

// url will be more or less like #....no effect
javascript:void(0)

 


 

Define an Object

// simple object
var myObject={};
myObject["name"]="pippo";
myObject["age"]="77";
console.log(myObject.name);

  


To Be Continued... 

Stay tuned, I will add more snippets soon...