检查是否使用 Javascript 加载了 jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7341865/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:53:16  来源:igfitidea点击:

Checking if jquery is loaded using Javascript

javascriptjquery

提问by SoftwareSavant

I am attempting to check if my Jquery Library is loaded onto my HTML page. I am checking to see if it works, but something is not right. Here is what I have:

我正在尝试检查我的 Jquery 库是否已加载到我的 HTML 页面上。我正在检查它是否有效,但有些不对劲。这是我所拥有的:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="/query-1.6.3.min.js"></script>
        <script type="text/javascript">
          $(document).ready(function(){
             if (jQuery) {  
               // jQuery is loaded  
               alert("Yeah!");
             } else {
               // jQuery is not loaded
               alert("Doesn't Work");
             }
          });
        </script>

回答by BoltClock

something is not right

有什么不对的

Well, you are using jQuery to check for the presence of jQuery. If jQuery isn't loaded then $()won't even run at all and your callback won't execute, unless you're using another library and that library happens to share the same $()syntax.

好吧,您正在使用 jQuery 来检查 jQuery 的存在。如果 jQuery 未加载,则$()根本不会运行,并且您的回调也不会执行,除非您使用另一个库并且该库碰巧共享相同的$()语法。

Remove your $(document).ready()(use something like window.onloadinstead):

删除你的$(document).ready()(改用类似的东西window.onload):

window.onload = function() {
    if (window.jQuery) {  
        // jQuery is loaded  
        alert("Yeah!");
    } else {
        // jQuery is not loaded
        alert("Doesn't Work");
    }
}

回答by Tushar Shukla

As per thislink:

根据这个链接:

if (typeof jQuery == 'undefined') {
    // jQuery IS NOT loaded, do stuff here.
}


there are a few more in comments of the link as well like,


链接的评论中还有一些,例如,

if (typeof jQuery == 'function') {...}

//or

if (typeof $== 'function') {...}

// or

if (jQuery) {
    alert("jquery is loaded");
} else {
    alert("Not loaded");
}


Hope this covers most of the good ways to get this thing done!!


希望这涵盖了完成这件事的大多数好方法!!

回答by Niko

if ('undefined' == typeof window.jQuery) {
    // jQuery not present
} else {
    // jQuery present
}

回答by Alexein

You can do this fast on the console-tab when inspecting your webpage.

检查网页时,您可以在控制台选项卡上快速执行此操作。

E.g:

例如:

$ === jQuery

If it returns trueit means it's loaded.

如果它返回,true则表示它已加载。

回答by Nika Tsilosani

Just a small modification that might actually solve the problem:

只是一个可能真正解决问题的小修改:

window.onload = function() {
   if (window.jQuery) {  
       // jQuery is loaded  
       alert("Yeah!");
   } else {
    location.reload();
   }
}

Instead of $(document).Ready(function()use window.onload = function().

而不是$(document).Ready(function()使用window.onload = function().

回答by V.Volkov

If jQueryis loading asynchronously, you can wait till it is defined, checking for it every period of time:

如果jQuery是异步加载,你可以等到它被定义,每一段时间检查一次:

(function() {
    var a = setInterval( function() {
        if ( typeof window.jQuery === 'undefined' ) {
            return;
        }
        clearInterval( a );

        console.log( 'jQuery is loaded' ); // call your function with jQuery instead of this
    }, 500 );
})();

This method can be used for any variable, you are waiting to appear.

此方法可用于任何变量,您正在等待出现。

回答by Bharadwaj

You can just type window.jQueryin Console . If it return a function(e,n) ... Then it is confirmed that the jquery is loaded and working successfully.

您只需输入 window.jQueryConsole 即可。如果它返回一个函数(e,n)......然后确认jquery已加载并成功运行。

回答by Jajikanth pydimarla

A quick way is to run a jQuery command in the developer console. On any browser hit F12 and try to access any of the element .

一种快速的方法是在开发人员控制台中运行 jQuery 命令。在任何浏览器上点击 F12 并尝试访问任何元素。

 $("#sideTab2").css("background-color", "yellow");

enter image description here

在此处输入图片说明