1

在我将由外部用户使用的 .js 文件中,我想检查是否在他们的页面上加载了 jQuery 库。为此,我正在使用:

if (!window.jQuery) {
    var script = document.createElement('script');
    script.type = "text/javascript";
    script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
    document.getElementsByTagName('head')[0].appendChild(script);
}    

$(document).ready(function () {
    //do some jquery $.ajax({}); stuff
});

但是,在$(document).ready(function () {我收到错误消息ReferenceError: $ is not defined如何解决它?

编辑:

我已经添加了行

if (!window.jQuery) {
    ...
    document.getElementsByTagName('head')[0].appendChild("<script type='text/javascript'>$(document).ready(function () {...});</script>");
} 

现在我得到了NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 0 [nsIDOMHTMLHeadElement.appendChild]

4

4 回答 4

1

在这种情况下,人们通常会被要求尝试类似require.js的东西……如果你不想使用它,你可以尝试设置自己的处理程序来捕捉 jquery 完成加载的时刻。

if (!window.jQuery) {
    var script = document.createElement('script');
    script.type = "text/javascript";
    script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
    document.getElementsByTagName('head')[0].appendChild(script);

    var loadHandlerAlreadyRun = false;
    script.onload = function() {
        if (!loadHandlerAlreadyRun) {
           loadHandlerAlreadyRun = true;
           onLoadHandler();
        }
    };
    script.onreadystatechange = function() {
        if (!loadHandlerAlreadyRun && (this.readyState === "loaded" || this.readyState === "complete")) {
            loadHandlerAlreadyRun = true;
            onLoadHandler();
        }
    }
}    

function onLoadHandler() {
    $(document).ready(function () {
        console.log("jquery loaded");
        //do some jquery $.ajax({}); stuff
    });

}

在这里试试

于 2012-12-04T12:08:41.613 回答
1

删除 document.ready 部分,并在您用于将 jQuery 附加到头部的脚本标签下方的单独脚本标签中,执行以下操作:

<script>
 window.onload = function() {
   //do the same code in here as you would've done in document.ready
 }
</script>

编辑

下面的评论提出了一个很好的观点......在这种情况下,你可以这样做:

<script>
 (function() {
     var load = function() {
       //do the same code in here as you would've done in document.ready
     };

     if(window.addEventListener) {
        window.addEventListener('load',load);
     } else {
        window.attachEvent('onload',load);
     }
 }());
</script>
于 2012-12-04T12:09:21.747 回答
0

你可以用代码检查:

if (typeof jQuery == 'undefined') {  
    // jQuery is not loaded
    var script = document.createElement('script');
    script.type = "text/javascript";
    script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js";
    document.getElementsByTagName('head')[0].appendChild(script);
}
于 2012-12-04T11:55:14.313 回答
0

像这样的东西

if( window.jQuery === undefined ) {
    //create script tag for appending script source
    var scriptTag = document.createElement("script");
    scriptTag.type = "text/javascript";
    //check for IE
    if( scriptTag.readyState ) {
        scriptTag.onreadystatechange = function() {
            if( scriptTag.readyState == 'loaded' || scriptTag.readyState == 'complete' ) {
                scriptTag.onreadystatechange = null;
                scriptHandler();
            }
        };
    }
    else {
        scriptTag.onload = function() {
            scriptHandler();
        };
    }
    scriptTag.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js";
    ( document.getElementsByTagName("head")[0] || document.documentElement ).appendChild( scriptTag );
}
else {
    jQuery = window.jQuery;
    process();
}

function scriptHandler() {
    jQuery = window.jQuery.noConflict( true );
    process();
}
function process() {
    jQuery(document).ready(function($) {
        //use $ here
    });
}
于 2012-12-04T12:11:27.300 回答