Optimalworks Ltd web design, SEO, training and consultancy 

the site completely surpassed my expectations GT, AutoDirector, we build websites that work, my website is great - I love it MP, XL Autos, reach more customers, make more sales, it's more than I ever could have expected LM, Trade Cars UK, we can help your business grow, 99% of business now comes from my website GV, Bash Events, promoting standards throughout the web industry, your contribution added a level of quality we would otherwise have struggled to achieve PS, Hemsley Fraser
  1. home
  2. Services
  3. Portfolio
  4. Contact us
  5. blog

JavaScript separation and the window.onload problem

written by Craig, 6 February 2007

Separating web page content (XHTML), layout (CSS), and behaviour (JavaScript) should be the goal for all decent web developers. It can be difficult to comprehend when you’re learning about web technology, but it makes your code clean and maintainable.

When is JavaScript run?

When you include or embed JavaScript in a page, code that exists outside of functions is run at that point. Since the remainder of the page has not loaded, your options are limited: for example, it is not possible to locate and manipulate a DOM element because it will not necessarily be available.

To solve this problem, we define an event which fires as soon as the page has loaded, for example: window.onload = RunMyCode;

The JavaScript can then apply lots of lovely effects safe in the knowledge that the page has fully loaded and the DOM is available.

The onload problem

Until the page has fully loaded, no progressive enhancement can be applied. This could be a problem if you have a slow connection or lots of content and images: the user is able to interact with the page before loading is complete and before any enhancements are applied.

Several workarounds are summarised in Peter Michaux’s excellent window.onload article. The solutions range from inline scripts at the bottom of the page to timers that periodically check for the availability of DOM elements.

The onload workarounds and their problems!

The proposed solutions are certainly clever, but none of them really feel right to me:

  • Manipulating DOM elements before the page has loaded is risky: there will still be limits to what you can achieve.
  • Testing and debugging multiple browsers will be more difficult. Events could fire at unknown times and not necessarily in the order you expect.
  • More JavaScript code is required, it increases complexity, and page load times are increased.

Revisiting the original issue

Users can interact with a page before it had fully loaded, but should this be an issue? Personally, I prefer a much simpler solution:

  1. Use progressive enhancement. If a page works without JavaScript, then it will still work before the window.onload event has fired.
  2. Use concise, semantic XHTML. Ensure the page size is small so the onload event fires as quickly as possible.
  3. Where necessary, specify images within CSS. Element backgrounds can be downloaded after the window.onload event has fired.
  4. If all else fails, hide your enhancements. If your widget absolutely requires JavaScript, then hide it using CSS (e.g. display:none). Once the page has loaded, your JavaScript can check for the hidden element and enable it as necessary.

Am I being overly cautious or simplistic? Does anyone have any further ideas?

  • categories: javascript
  • tags: dom, javascript
  • trackback: http://www.optimalworks.net/blog/2007/web-development/javascript/window-onload-problem/trackback
  • bookmark: del.icio.us, digg, facebook, reddit, Furl, Spurl, Blinklist, Slashdot, Technorati, Yahoo!
add your comments
please enter your name
please enter your email address - it will not be published anywhere
please enter your website address
please enter your comments (all are moderated and checked for spam)
  • next article: Oranges, text sizes, and cliches 
  • previous article: 7 tips for web graphic designers 
search
  • all popular (5)
  • courses (1)
  • general (3)
  • hardware (1)
  • software (24)
    • free download (6)
    • web browsers (9)
    • web servers (2)
  • web development (31)
    • accessibility (4)
    • ajax (1)
    • css (2)
    • graphic design (4)
    • html (1)
    • javascript (7)
    • php (4)
    • seo (1)

tags

  • accessibility
  • ajax
  • apache
  • award
  • computing
  • css
  • design
  • developers
  • dom
  • download
  • editor
  • firefox
  • funny
  • graphics
  • hardware
  • html
  • ie
  • javascript
  • opera
  • php
  • portfolio
  • review
  • safari
  • seo
  • server
  • software
  • ssl
  • standards
  • wordpress
  • xml
  1. site map
  2. home
  3. Services
  4. Portfolio
  5. Contact us
  6. blog
  7. RSS feed
  8. project survey

XHTML 1.0 | CSS 2.1 | WAI AAA | printer-friendly

©2008 Optimalworks Ltd, Devon, UK. Registered in England and Wales No. 5922205.

This page can be viewed at http://www.optimalworks.net/blog/2007/web-development/javascript/window-onload-problem