Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

javascript - 检查是否使用Javascript加载了jquery(Checking if jquery is loaded using Javascript)

I am attempting to check if my Jquery Library is loaded onto my HTML page.

(我正在尝试检查我的Jquery库是否已加载到我的HTML页面上。)

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>
  ask by SoftwareSavant translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

something is not right

(事情不对)

Well, you are using jQuery to check for the presence of jQuery.

(好吧,您正在使用jQuery检查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,则$()甚至都不会运行,并且回调也不会执行,除非您使用的是另一个库,并且该库恰好共享相同的$()语法。)

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...