beuleal

why not use window.onload?

Recommended Posts

window.onload is only needed if you include your JavaScript at the top of the html page (i.e. in the <head> tag) - you'd do this to load your JavaScript up-front to do something important with your page before it's loaded. If you don't need to do this (which turns out to be the case most of the time) you can put your JavaScript at the bottom of the page (usually just before the closing </body> tag) and this will mean by the time the JavaScript loads, the rest of the page has already loaded.

 

It's better practice to put your JavaScript at the end of the page, as this makes the page perceptibly faster to open - the browser will render the page as soon as it has enough to make sense of the contents, but loading script files 'blocks' the browser until they've loaded. By having the browser render the page first, then start loading and executing the scripts, the user spends less time watching a white page loading.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.