2011-03-21 112 views
0

爲了儘可能快,我現在就跳到這個話題。 我想在加載jQuery之前延遲腳本。如何暫停JavaScript,直到完成任何事情?

這裏是我的問題:我有代碼時,jQuery是尚未加載會自動插入jQuery.js

if(typeof jQuery=="undefined") { 
    var s=document.createElement("script"); 
    s.type="text/javascript"; 
    s.async=true; 
    s.src="http://code.jquery.com/jquery-1.5.1.min.js"; 
    var x=document.getElementsByTagName("script")[0]; 
    x.parentNode.insertBefore(s, x); 
} 

$(document).ready(function() { 
    //My code goes here, right? 
}); 

它完全插入腳本,但問題是$(document).ready()不等到腳本被完全加載,它在腳本加載時立即跳下來。我想在那裏停下來,我該怎麼辦?

+2

我不太關注。 $(document).ready是一個jQuery特性,所以如果jQuery不存在,代碼將無法工作。你能發佈一個更完整的例子嗎? – 2011-03-21 23:48:59

+0

@cwolves:我覺得OP是想說,他希望前'$(文件)。就緒暫停執行(...)'直到jQuery是加載中... – Cameron 2011-03-21 23:56:01

+0

@Cwolves和卡梅隆:我想暫停$(document).read(...)或者確切地說,我想在加載jQuery之前暫停整個腳本。謝謝您的回覆。 [X] – xx3004 2011-03-22 15:18:25

回答

2

就像在評論中提到cwolves,這不應該是一個問題 - 加載jQuery的時候$(document).ready()應該只工作。

但是,如果你發現你需要等到它的加載,可以使這樣的事情:

if(typeof jQuery=="undefined") 
{ 
    var s=document.createElement("script"); 
    s.type="text/javascript"; 
    s.async=true; 
    s.src="http://code.jquery.com/jquery-1.5.1.min.js"; 
    var x=document.getElementsByTagName("script")[0]; 
    x.parentNode.insertBefore(s, x); 
    wait(); 
} 

//... 

function wait() { 
    if(typeof jQuery=="undefined") 
    { 
     setTimeout(wait, 200); 
    } 
    else { 
     //jQuery is loaded, do what you need to 
     $(document).ready(docLoaded); 
    } 
} 

從這篇文章改編約loading jQuery with Greasemonkey scripts

+0

這似乎是個好主意,但它使我的劇本反應遲鈍:-([X] – xx3004 2011-03-22 21:40:09

+0

嗯,這是奇怪的,我不知道爲什麼,可能是。您是否嘗試過增加超時延遲到更多的東西超過200? – 2011-03-22 21:53:54

0

可以使用window.setInterval來輪詢jQuery的狀態:

(function() { 
    function main() { 
     // code to continue with 
    } 

    function jQueryLoaded() { 
     // test jQuery status here 
    } 

    var handle = window.setInterval( 
     function() { 
      if (jQueryLoaded()) { 
       window.clearInterval(handle) 
       main() 
      } 
     }, 1000); // 1000 msec interval 

})(); 
+0

你的setInterval,它仍然繼續前進到下一個腳本,而間隔爲流鼻涕,並:-( [X] – xx3004 2011-03-22 20:51:21

0

啊,gotcha。這額外位幫助:)

你想要的是你的代碼依賴於jQuery是加載的jQuery時執行。要做到這一點,我們使用腳本的onload事件,所以:

function toBeExecuted(){ 
    // code goes here 
} 

if(!jQuery){ 
    var s = document.createElement("script"); 
    s.type = "text/javascript"; 
    s.src = "http://code.jquery.com/jquery-1.5.1.min.js"; 
    s.onload = toBeExecuted; 

    // because IE just HAS to be different 
    s.onreadystatechange = function() { 
     if(s.readyState == 'loaded' || s.readyState == 'complete'){ 
      toBeExecuted(); 
     } 
    } 

    var x = document.getElementsByTagName("script")[0]; 
    document.getElementsByTagName('HEAD')[0].appendChild(s); 
}else{ 
    toBeExecuted(); 
} 
+0

呃,這個問題是我想單獨使腳本不暫停腳本時,意味着我只是複製它並粘貼到任何頁面上,如果我做了上述方法,我將不得不改變toBeExecuted()函數,這可能會混淆我的腳本,謝謝你的幫助,你還有什麼想法嗎? [x] – xx3004 2011-03-22 20:53:49

+0

你的腳本進入toBeExecuted函數。其他的東西只是讓它在jQuery加載時立即運行,而不是一次又一次地檢查它是否被加載。 – 2011-03-22 21:46:31

相關問題