Reputation: 15
I luckily found this code on the net for continuos add on onload from a different subject completely
function myPluginLoadEvent(func) {
// assign any pre-defined functions on 'window.onload' to a variable
var oldOnLoad = window.onload;
// if there is not any function hooked to it
if (typeof window.onload != 'function') {
// you can hook your function with it
window.onload = func
} else { // someone already hooked a function
window.onload = function () {
// call the function hooked already
oldOnLoad();
// call your awesome function
func();
}
}
}
// pass the function you want to call at 'window.onload', in the function defined above
myPluginLoadEvent(func);
However, this only allow for one add on onload call. How do I loop this for multiple on load call such as for i, 1++ ? Many thanks in advance.
Upvotes: 0
Views: 407
Reputation: 781833
The code you posted will work repeatedly. Each time it's called, oldOnLoad
contains whatever was previously assigned to window.onload
, which could be the result of a previous use. So it just keeps chaining all the functions.
However, the modern approach is to use addEventListener
rather than assigning to window.onload
.
window.addEventListener("load", func);
Upvotes: 1