Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, September 26, 2011

Best way to check for an empty string in JavaScript


For checking if a string is empty, null or undefined use:

function isEmpty(str){
   return (!str || 0===str.length);
}
For checking if a string is blank (with no tabs and spaces):

if(str.replace(/\s/g,"") == ""){
}

Thursday, June 16, 2011

Tip: Grails variable in javascript

A grails var can be accessed in javascript using $ notation

var rsData = '${ruleSetData}';
If you want to convert it to JSON use encodeAsJSON()
 
var objJSONData =  '${gObject.encodeAsJSON()}' 
and parse this object in javascript using
var parsedJSONData = eval(objJSONData); 
This obj can then be used in JS as
for(var i=0; i< parsedJSONData.length; i++){
  alert(parsedJSONData[i].propertyName);
}