Possible Duplicate:
How can I check whether a variable is defined in JavaScript?
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
我的 playbook 分为两部分.
The first part sets up a var:
var pagetype = "textpage";
The 2nd part is a simple if statement:
if(pagetype == "textpage") {
//do something
};
Now the 2nd part, the if statement, appears on all pages of my site. But the first part, where the var is declared, only appears on some of my pages.
在没有var的页面上,我自然会遇到以下错误:
Uncaught ReferenceError: pagetype is not defined
所以我的问题是:有没有一种使用JavaScript或JQ的方法来检测变量是否存在(不仅仅是检测是否有数据分配给它)?
I am imagining I would just use another if statment, eg:
if ("a var called pagetypes exists")....