2015-11-02 56 views
1

我想從美元符號已經分配給某個函數的網站上的Tampermonkey(/ Greasmonkey)腳本加載jQuery。 所以我想知道:是否可以加載jQ而無需重新定義$? 我想這是不可能的,但也許缺少IMA ... somethng當美元符號已經定義時加載jQuery

+1

http://stackoverflow.com/questions/6746352/replace-dollar-sign-with-jquery –

+2

或者你可以刪除'// @grant none'從而使腳本將在其自己的沙箱中運行。 – wOxxOm

回答

0

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);