How can I run a JavaScript code when the page loads without using the onload tag?
I\'m trying to run this script:
You can do
A cleaner way would be
since this allows for multiple eventhandlers to the same event
A better way is usually to react to domReady though.
Most javascript libraries support easy ways to hook into domReady. In jQuery for example you can do it like this:
$(function() {
/* code here will be run as soon as the dom is ready to be interacted with,
but before document.load occurs which means you don't have to wait for
images to load for example
*/
});