If you want to display the alrert window or use window.onload, it makes no sense to use both, here is the code that will work fine
window.onload (which is implemented even in older browsers), which starts when the whole page loads
window.onload = function(){ alert('test'); }
jQuery provides document.ready , which abstracts them, and fires as soon as the DOM page is ready
$(document).ready(function () { alert('Test'); });
Check the answer: window.onload vs $ (document) .ready ()
window.onload is a built-in Javascript event, but since its implementation had subtle quirks in browsers (FF / IE6 / IE8 / Opera), jQuery provides a document.ready that abstracts them and fires as soon as the DOM page is ready (no images wait etc.).
document.ready is a jQuery function wrapping and ensuring consistency with the following events:
document.ondomcontentready / document.ondomcontentloaded - a new event that is fired when a DOM document is loaded (which may take some time before loading images, etc.); again, is slightly different in IE and the rest of the world.- and window.onload (which is implemented even in older browsers), which starts when the entire page loads (images, styles, etc.).
source share