English 中文(简体)
检测带有普通刺绣的Jquery, 如果不是在现场的话, 则用活性载荷检测 Jquery 。
原标题:Detect Jquery with regular javascript, if not present load it dynamcally

我需要密码来使用常规的 Javascript 来检测 JQuery 是否存在, 如果没有的话, 从 Google 或其他网站装入 JQuery 文件

更新两个工作解决方案(仅在此复制和粘贴工作代码):

来自克劳迪奥·雷迪

window.jQuery || document.write("<script src= https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js >x3C/script>")

来自罗布达尔文

var jQueryScriptOutputted = false;
function initJQuery() {
    if (typeof(jQuery) ==  undefined ) {
        if (! jQueryScriptOutputted) {
            jQueryScriptOutputted = true;
            document.write("<scr" + "ipt type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    }
}
initJQuery();
最佳回答

http://weblogs.asp.net/joelvarty/archive/2009/05/07/load-jquery-hirdalally.aspx" rel = “nofollow” > this 应该有效。

EDIT: 从上面链接添加的代码 。

var jQueryScriptOutputted = false;
function initJQuery() {

    //if the jQuery object isn t available
    if (typeof(jQuery) ==  undefined ) {


        if (! jQueryScriptOutputted) {
            //only output the script once..
            jQueryScriptOutputted = true;

            //output the script (load it from google api)
            document.write("<scr" + "ipt type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    } else {

        $(function() {  
            //do anything that needs to be done on document.ready
        });
    }

}
initJQuery();
问题回答

有很多种方法,我最喜欢这个 只是因为越少的动词

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>  
<script>window.jQuery || 
    document.write("<script src= js/jquery-1.7.2.min.js >x3C/script>")
</script>   

在不使用 < code> document. write 的情况下装入 jQuery 的 < em> unice 方式是:

if (window.jQuery === undefined) {
    var s = document.createElement( script );
    s.src = "//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js";
    document.head.appendChild(s);
}

这将造成非同步的负载, 不过 - 所以您可能想要包含一个 < code>. onload 处理器, 以便让执行等待负荷完成 。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签