2011-03-21 114 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

相关问题