簡體   English   中英

頁面最初加載后加載jQuery庫

[英]load jQuery library after page has initially loaded

我有一種情況,我需要檢查頁面上是否存在jQuery,如果不存在,則需要加載它。 我創建一個腳本標簽並引用外部jQuery庫。 在繼續執行其余腳本之前,如何等待頁面上加載庫?

更新:

以下是G園區的建議之后的樣子:

if (typeof jQuery === 'undefined') {
    var j = document.createElement('SCRIPT');
    j.type = 'text/javascript';
    j.src = '//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
    document.getElementsByTagName('head')[0].appendChild(j);
    j.onload = function () {
        var section = document.createElement('DIV');
        section.setAttribute('style', 'height:300px; width:250px; position: fixed; right: 0px; top: 100px;z-index: 9999; border-style:solid;border-width:1px;');
        section.innerHTML = '<p>steps</p><textarea name="steps"></textarea><p>expected results</p><textarea name="results"></textarea><p><input type="button" name="submit" value="add test case" /></p>';

        document.getElementsByTagName('body')[0].appendChild(section);
    };  
} 
<script type="text/javascript">
    if(typeof jQuery === 'undefined')
        document.write('<sc'+'ript type="text/javascript" src="jquery.js"></scrip'++'t>');
    window.onload = function(){
        // jquery should be present here
    });
</script>

您可以檢查是否像這樣加載了jquery

window.onload = function(){
    if(typeof jQuery == "undefined"){
           // jquery is not available 
    }
}

如果未加載jquery,我建議您使用JavaScript加載器,例如requirejs http://requirejs.org/

您可以將其設置為window.onload

window.onload = function () { alert("It's loaded!") }

頁面完全加載后執行Javascript

包含jQuery文件,僅在需要時才需要檢查它

function afterLoad(){
  if(typeof(jQuery)!=='undefined')
    return;

  var element1 = document.createElement(“script”);
  element1.src = “pointToYourjQueryFile.js”;
  element1.type=”text/javascript”;
  document.getElementsByTagName(“head”)[0].appendChild(element1);
}

以這種方式在窗口加載時調用此函數...

<body onload="afterLoad()"?>
window.onload = function(){
    var script,head;
    if(!jQuery){
        script = document.createElement('script');
        document.getElementsByTagName('head')[0].appendChild(script);
        script.onload = function(){
           alert('jQuery loaded');
        }
        script.src = 'path_to_jquery';
    }
}

Modernizr使用以下行:

<script>window.jQuery || document.write('<script src="./js/libs/jquery-1.6.2.min.js"><\/script>')</script>

但是document.write被認為是不安全的。 因此,我將其與已發布的內容連接起來:

if(!window.jQuery)
{
    var element1 = document.createElement(“script”);
    element1.src = “somefile.js”;
    element1.type=”text/javascript”;
    document.getElementsByTagName(“head”)[0].appendChild(element1);
}

另一方面,Google使用:

(function() {
    if(window.jQuery) return;

    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'some.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

它將腳本附加到網站上已經存在的第一個標簽中。 如果您的站點上至少有一個<script>,請使用該腳本。 據我所知,這被認為是最好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM