检查是否使用Javascript加载了jquery [英] Checking if jquery is loaded using Javascript

查看:128
本文介绍了检查是否使用Javascript加载了jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查我的Jquery库是否已加载到我的HTML页面上。我正在检查它是否有效,但有些事情是不对的。以下是我的内容:

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:

<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>


推荐答案


有些不对劲

something is not right

好吧,你正在使用jQuery检查是否存在jQuery。如果没有加载jQuery,那么 $()甚至根本不会运行,并且你的回调将不会执行,除非你正在使用另一个库并且该库恰好共享相同的 $()语法。

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.

删除 $(文档).ready ()(使用类似 window.onload 的代码):

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

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

这篇关于检查是否使用Javascript加载了jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆