Jump to content

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.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...