Saturday, January 16, 2010

Check if JavaScript Variable is Defined

function isdefined(variable)
{
return (typeof(variable) != ‘undefined’);
}


Courtesy of: http://jehiah.cz/archive/javascript-isdefined-function (and after testing what worked for me)

Note: there's interesting quirk in IE wherein if there's a div/span with the same id (for example, you want to check if the variable "test1" exists, and you have a span with the id of "test1"), then typeof(test1) will return "object", which can obviously lead to unwanted results.

No comments:

Post a Comment