Skip to content

Instantly share code, notes, and snippets.

@koraysels
Created February 20, 2015 12:54
Show Gist options
  • Save koraysels/87d15204f4e2ad89ebec to your computer and use it in GitHub Desktop.
Save koraysels/87d15204f4e2ad89ebec to your computer and use it in GitHub Desktop.
the correct way to use window.onload
module.exports = function windowOnLoad(func) {
// pass the function you want to call at 'window.onload', in the function defined above
// 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();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment