1

I wanna to load jQuery from the Tampermonkey(/Greasmonkey) script on the site where dollar sign already assigned to some function. So i wonder: is it possible to load jQ without redefining $? I guess it's impossible, but maybe ima missing somethng...

Markus_13
  • 328
  • 2
  • 14

1 Answers1

0

Thx for comments, i didn't knew about jQuery.noConflict(); So I came up with this code afterall:

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

I know it's dirty, but it works. Used like this: jqChkLoad( 0, function(){ alert('callback!'); } ); jqChkLoad('1.8.3'); jqChkLoad(0, some_func); etc.

Markus_13
  • 328
  • 2
  • 14