Technology Tales

Adventures in consumer and enterprise technology

The dangers of overriding JavaScript onload event handlers

Published on 17th April 2008 Estimated Reading Time: 2 minutes

Recently, I gave myself a right old fright while tinkering with my hillwalking and photo gallery website. The problem stemmed from my use of window.onload to set up behaviours for web pages in a visitor information directory on the site. A lapse of concentration allowed me to associate an onload event handler with the body tags of the pages using a common header PHP script; another lapse also meant that my mistake was on public view for all to see because I uploaded files before I spotted the problem.

The result was that I was left wondering why the window.onload pieces weren't working at all, something that seriously broke the pages. The mists of panic and bewilderment were cleared in good time by the realisation of what had happened: the body tag onload had overridden the window onload and rendered it inactive. I don't know from where the thought arrived, but it was the one that resolved the problems that I was seeing; it might be that I might have met it before in the dim and not-so-distance past.

Having your pages degrade gracefully for when a visitor has not enabled JavaScript or when you are foolish enough to break something like I did is definitely an asset, a point brought home to me by my salutary experience. I am not sure why I was willing to run the risk that I did, but it now looks as if I need to include the task of adding improved graceful degradation to the to-do list.

  • The content, images, and materials on this website are protected by copyright law and may not be reproduced, distributed, transmitted, displayed, or published in any form without the prior written permission of the copyright holder. All trademarks, logos, and brand names mentioned on this website are the property of their respective owners. Unauthorised use or duplication of these materials may violate copyright, trademark and other applicable laws, and could result in criminal or civil penalties.

  • All comments on this website are moderated and should contribute meaningfully to the discussion. We welcome diverse viewpoints expressed respectfully, but reserve the right to remove any comments containing hate speech, profanity, personal attacks, spam, promotional content or other inappropriate material without notice. Please note that comment moderation may take up to 24 hours, and that repeatedly violating these guidelines may result in being banned from future participation.

  • By submitting a comment, you grant us the right to publish and edit it as needed, whilst retaining your ownership of the content. Your email address will never be published or shared, though it is required for moderation purposes.