Archive

cross-domain AJAX with dynamic script tag

This code is right from the book "High Performance JavaScript." It lets you do AJAX-like requests cross-domain, and since you can by-pass eval of JSON, by using P-JSON (padded JSON), it actually performs much faster than classic AJAX.

var loadScript = function(url, callBack){

    //create element
    var script = document.createElement("script");
    script.type = "text/javascript";
    
    if(script.readyState){ //for IE
        
        script.onreadystatechange = function(){
            
            if(script.readyState == "loaded" || script.readyState == "complete"){
                script.onreadystatechange = null;
                callBack();
            }
        
        }
                
    }
    else{ //other browsers
        
        script.onload = function(){
            callBack();
        }

    }
    
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);

};

Comments:

mbt shoes
8/4/2010 1:39 PM
Hey I think you have a great blog going here, I found it on Bing and plan on returning regularly for the information that you all are providing.

free registry cleaner
8/5/2010 5:59 PM
Thank you for useful info. :-)

make money easy
8/10/2010 8:08 PM
One again, your articles is very good.thank you!very much.

finance software
8/10/2010 11:01 PM
nice to be here.... thanks for share