我遇到一些麻煩,在這裏,我加載jQuery的,如果它尚未才能運行加載我的腳本。我的腳本被插入別人網站的body標籤中。 我的問題是,jquery與mootools衝突只是加載它(jQuery)。
這裏是我的腳本:
var attemptCount=0;
if(typeof jQuery=='undefined') {
var script=document.createElement('script');
script.type='text/javascript';
script.src='https://ajax.googleapis.com/ajax/.../1.6.2/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
}
function init(){
if(arguments.callee.done) return;
if(typeof jQuery!='undefined'){
jQuery.noConflict();
arguments.callee.done=true;
// do some jquery stuff i.e. jQuery('...').etc...
}
}
function waitForJQuery(){
if(typeof jQuery!='undefined'){
init();
return;
}
if(attemptCount<100){
setTimeout(waitForJQuery,100);
}
return;
}
if(document.addEventListener){
document.addEventListener("DOMContentLoaded",init,false);
}
else if(document.attachEvent){
waitForJQuery();
}
window.onload=init;
任何幫助將非常感激。
注意:您可以使用`script.onload = init;方法`你不需要使用`的addEventListener()`和`的setTimeout( )`。 – kubetz 2011-12-15 08:52:34
謝謝,我會試試看。 – Sparkup 2011-12-15 09:00:47