Search This Blog

30 December 2010

How to check if a javascript global variable is defined or not in a javascript function?

We can do it with following code,

if (variablename == undefined){
        variablename = 'XYZ';
}

But for some reason, firefox will throws error with above code. So the solution to that problem is,
if(window.variablename == undefined){
       window.variablename = 'XYZ';
}

No comments:

Post a Comment