2011-05-26 28 views
2

我有一个小部件被添加到随机网站。如何使用来自Widget代码的网站jQuery

每当这些网站有jQuery,我希望我的代码使用它,而不是再次上传jQuery。

如何通过一次性设置,我的函数调用jQuery调用网站的jQuery,可能带有不同的jQuery前缀($)?

回答

0

使用匿名函数来测试jQuery对象的存在。如果未定义,请将其添加到DOM中,然后再重试一次:

void(function() 
    { 
    if (/function/.test(this["jQuery"]) == false) 
    { 
    var anon = arguments.callee; 
    var jqs = document.createElement("script"); 
    jqs.src = "http://code.jquery.com/jquery-latest.js"; 
    document.getElementsByTagName("body")[0].appendChild(jqs); 

    window.setTimeout(function() {anon();}, 500); 
    } 
    else 
     { 
     $(document).ready(function(){document.title = "jQuery"}); 
     } 
    } 
());