1
我想从美元符号已经分配给某个函数的网站上的Tampermonkey(/ Greasmonkey)脚本加载jQuery。 所以我想知道:是否可以加载jQ而无需重新定义$? 我想这是不可能的,但也许缺少IMA ... somethng当美元符号已经定义时加载jQuery
我想从美元符号已经分配给某个函数的网站上的Tampermonkey(/ Greasmonkey)脚本加载jQuery。 所以我想知道:是否可以加载jQ而无需重新定义$? 我想这是不可能的,但也许缺少IMA ... somethng当美元符号已经定义时加载jQuery
THX征求意见,我不知道jQuery.noConflict();
所以我想出了这个代码毕竟:
function isFunc(a){
return((a) && (typeof a==='function'));
}
function jsL0ad(url,id,cllbck){
if(!url) return((isFunc(cllbck))?cllbck():undefined);
var hd=document.getElementsByTagName('head')[0],r=false;
if(!hd) return((isFunc(cllbck))?cllbck():undefined);
var s=document.createElement('script');
s.type='text/javascript';
s.src=url;
if(id!=undefined) s.id=id;
s.onload=s.onreadystatechange=function(){
if((!r)&&((!this.readyState)||(this.readyState=='loaded')||(this.readyState=='complete'))){
r=true;
if(isFunc(cllbck)) cllbck();
s.onload=s.onreadystatechange=null;
hd.removeChild(s);
}
};
hd.appendChild(s);
}
function jqChkLoad(v,cllbck){
if(window.jQuery) return((isFunc(cllbck))?cllbck():undefined);
if(!v) v='2.1.3';
var cllbck1=cllbck;
if(window.$){
cllbck1=function(){
jQuery.noConflict();
if(isFunc(cllbck)) cllbck();
};
}
jsL0ad('//ajax.googleapis.com/ajax/libs/jquery/'+v+'/jquery.min.js',undefined,cllbck1);
}
我知道它的肮脏,但它的工作原理。 像这样来使用:jqChkLoad(0, function(){ alert('callback!'); });
jqChkLoad('1.8.3');
jqChkLoad(0, some_func);
等
http://stackoverflow.com/questions/6746352/replace-dollar-sign-with-jquery –
或者你可以删除'// @grant none'从而使脚本将在其自己的沙箱中运行。 – wOxxOm