How do we have a Javascript callback when the page is loaded?

How do we have a Javascript callback when the page is loaded?

And why does it matter? This is a useful way to initialize a web application with at the start of loading a website or web application.

// A minimal wrapper so that if we need to support older browsers we can function READYevent(Func){ if(document.readyState !== 'complete') { window.onload = Func; return; } else { Func(); } }

We need to check the ready state as the page may already be loaded by the time we try to set the listener.

jQuery.ready does this and also has extra code for legacy browsers with for simplicity we are avoiding.

Other reading