Add Window OnLoad Event (plain Javascript)

If you run into a situation where you need to add a window.onload event, and you cannot rely on a library to do the work (no jQuery), you can use old school, plain javascript, here is what you can do:

function plainJSWindowOnload() {
    console.log('Yay!');
}


     'attachEvent' in window && window.attachEvent('onload', plainJSWindowOnload, false);
'addEventListener' in window && window.addEventListener('load', plainJSWindowOnload, false);

Leave a Reply