Created
February 20, 2015 12:54
-
-
Save koraysels/87d15204f4e2ad89ebec to your computer and use it in GitHub Desktop.
the correct way to use window.onload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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