javascript 如何确定网页是否启用了 jquery?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8631333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if a web page is jquery-enabled?
提问by Jim_CS
What is the best way to determine if a webpage is jquery enabled? Using jquery itself if that's the best way to determine it.
确定网页是否启用 jquery 的最佳方法是什么?如果这是确定它的最佳方法,则使用 jquery 本身。
回答by Calvin Froedge
if(jQuery) //jquery object exists
jQuery isn't magic - it's essentially just a big object. You can check for it like you would any other object.
jQuery 并不神奇——它本质上只是一个大对象。您可以像检查任何其他对象一样检查它。
Same thing to ensure libraries within jQuery are loaded:
确保加载 jQuery 中的库的相同方法:
if(jQuery.DatePicker) //lib exists
回答by Rich
The best way to check if jQuery is loaded is
检查 jQuery 是否已加载的最佳方法是
if (window.jQuery) {
// jQuery is loaded
} else {
// jQuery is not loaded
}
If you check using if(jQuery){}
, and it isn't there, you are going to get a reference error like below, and it will break the execution of your script. By checking if the window object has a property called jQuery, if it isn't there, it will simply return undefined.
如果您检查 using if(jQuery){}
,但它不存在,您将收到如下所示的参考错误,它会中断您的脚本的执行。通过检查 window 对象是否有一个名为 jQuery 的属性,如果它不存在,它将简单地返回 undefined。
回答by jimmyplaysdrums
Run this in the console:
在控制台中运行:
if (window.jQuery) {
console.log("Yes there's jQuery!");
} else {
console.log("Nope, it's not on this site...");
};
回答by Matt Seymour
do a check to see if the javascript object is initialised.
检查 javascript 对象是否已初始化。
if(jQuery)
{
alert('jquery active');
}
回答by Matt Seymour
Check in javascript if the jquery object exists.
如果 jquery 对象存在,请检查 javascript。
Use the below if condition to check if jQuery object exists.
使用下面的 if 条件来检查 jQuery 对象是否存在。
if(jQuery)
{
//jquery object exists
}
回答by JaredMcAteer
if (jQuery) { // yup it's there }
if (jQuery) { // yup it's there }