JavaScript onload/onreadystatechange not firing when dynamically adding a script tag to the page.
        Posted  
        
            by spoon16
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by spoon16
        
        
        
        Published on 2010-05-18T16:51:16Z
        Indexed on 
            2010/05/18
            18:20 UTC
        
        
        Read the original article
        Hit count: 361
        
JavaScript
|events
I am developing a bookmarklet that requires a specific version of jQuery be loaded on the page. When I have to dynamically insert a jQuery script tag to meet the requirments of the bookmarklet I want to wait for the onload or onreadystatechange event on the script tag before executing any function that requires jQuery.
For some reason the onload and/or onreadystatechange events do not fire. Any ideas on what I am doing wrong here?
var tag = document.createElement("script");
tag.type = "text/javascript";
tag.src = "http://ajax.microsoft.com/ajax/jquery/jquery-" + version + ".min.js";
tag.onload = tag.onreadystatechange = function () {
    __log("info", "test");
    __log("info", this.readyState);
};
document.getElementsByTagName('head')[0].appendChild(tag);
The FULL code: http://gist.github.com/405215
© Stack Overflow or respective owner