user4756836
user4756836

Reputation: 1337

Having multiple window.onload functions

I am having to use multiple window.onload functions on one page because the JS plugin that I am using is calling the window.onload function already. I would like to not edit the plugin and work around it. Here is what I have tried:

<div id="Count"></div>

//<script type="text/javascript" src="plugin.js" /> // JS plugin using window.onload

var INCREMENT = 3;
var count = 0;
var msInterval = 800;
var oldonload = window.onload; // assigning current window.onload to a variable

window.onload = function() { <-- error line
  if (oldonload) {
    oldonload();
  }
  document.getElementById('Count').innerHTML = count;
  setInterval("count += INCREMENT; document.getElementById('Count').innerHTML = count;", msInterval);
}

I am getting this error:

TypeError: (intermediate value)(...) is not a function

Upvotes: 2

Views: 1682

Answers (1)

Andrew Jenkins
Andrew Jenkins

Reputation: 1609

Try using addEventListener instead.

function myFunction() {
    // do stuff here
}
window.addEventListener("load", myFunction, false);

Upvotes: 5

Related Questions