0

Possible Duplicate:
Adding multiple onload handlers

can i add another onload callback on existing onload?

i have implemented bigpipe on the site and resources (javascript) gets loaded on demand and after javascript is loaded properly, then javascript code gets executed.

this is javascript code i have

var code = document.createElement('script');
 code.setAttribute("type", "text/javascript");
 code.setAttribute("src", this.file);
 code.onload = this.onComplete.bind(this);
 code.onreadystatechange = code.onload; // ie fix

after this code is executed, can i add another onload callback on top of this, so they both get triggered, once this resource gets done loading?

Community
  • 1
  • 1
Basit
  • 16,316
  • 31
  • 93
  • 154
  • 2
    You may find this question and answer useful http://stackoverflow.com/questions/4558325/adding-multiple-onload-handlers – Pebbl Nov 12 '12 at 07:49
  • i was testing same way, just now and yes this is very useful. im gonna test some more and implement it, if everything goes fine. – Basit Nov 12 '12 at 07:56
  • @pebbl Thank you. i have posted answer, so others can use it too. – Basit Nov 12 '12 at 07:59

1 Answers1

0
function onDone ()
{
    alert('im done');
}

function onComplete ()
{
    alert('im completed');
}

var code = document.createElement('script');
code.setAttribute("type", "text/javascript");
code.setAttribute("src", '/js/jquery.js');
code.setAttribute("id", 'jquery');
code.onload = onComplete.bind(this);
document.getElementsByTagName("head")[0].appendChild(code);


resource = document.getElementById('jquery');
onloadPrevious = resource.onload;
resource.onload = function () {

     if (typeof onloadPrevious == "function")
         onloadPrevious();
     onDone();

}
Basit
  • 16,316
  • 31
  • 93
  • 154