admin管理员组文章数量:1194605
I always thought I could just check an undefined var by comparing it to undefined, but this is the error I get in the chrome console :
How how do i check for the object jQuery being undefined ?
EDIT :
if(jQuery) is giving me problems too
EDIT :
solution :
if(window.jQuery)
works.
typeof(jQuery) == 'undefined'
works too.
Could anyone explain why ?
I always thought I could just check an undefined var by comparing it to undefined, but this is the error I get in the chrome console :
How how do i check for the object jQuery being undefined ?
EDIT :
if(jQuery) is giving me problems too
EDIT :
solution :
if(window.jQuery)
works.
typeof(jQuery) == 'undefined'
works too.
Could anyone explain why ?
Share Improve this question edited Dec 16, 2011 at 7:31 YD8877 asked Dec 16, 2011 at 7:23 YD8877YD8877 10.8k20 gold badges69 silver badges97 bronze badges 2- [stackoverflow.com/questions/8531059/… [1]: stackoverflow.com/questions/8531059/… – Eric Yin Commented Dec 16, 2011 at 7:40
- stackoverflow.com/questions/27509/… , stackoverflow.com/questions/8531059/… – user166390 Commented Dec 16, 2011 at 7:49
7 Answers
Reset to default 9There are several solutions:
Use
typeof
. It is a special operator and will never result in aReferenceError
. It evaluates to "undefined" for, well, theundefined
value or for a variable which does not exist in context. I'm not a fan of it, but it seems very common.Use
window.jQuery
. This forces a "property lookup": property lookups never fail, and returnundefined
if said property does not exist. I've seen it used in some frameworks. Has the downside of assuming a context (usuallywindow
).Make sure the variable is "declared":
var jQuery; if (jQuery) { /* yay */ }
. Doesn't seem very common, but it is entirely valid. Note thatvar
is just an annotation and is hoisted. In the global context this will create the "jQuery" property.Catch the
ReferenceError
. Honestly, I have never seen this nor do I recommend it, but it would work.
Happy coding.
Process 1:
if (jQuery) {
// jQuery is loaded
} else {
// jQuery is not loaded
}
Process 2:
if (typeof jQuery == 'undefined') {
// jQuery is not loaded
} else {
// jQuery is loaded
}
From here
if(typeof jQuery == "undefined") {
document.write("undefined");
}else{
document.write("Exists");
}
As far as I know you can do
if(typeof X == 'undefined')
But there are resources loader you might want to take a look at. And the answer given before me is also correct.
The variable called "jQuery" in your code has never been declared, so it will throw an error like "xxx(variable name) is not defined".
You can use the typeof
operator to check is a variable is undefined or not
if (typeof(jQuery) == "undefined")
You can use : if( typeof jQuery !== 'undefined')
or
Do what is recommended by mozilla
if('jQuery' in window)
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined
if(jQuery)
should be enough, shouldn't it?
本文标签: javascriptHow to check if a variable or object is undefinedStack Overflow
版权声明:本文标题:javascript - How to check if a variable or object is undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738506861a2090584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论