Technology Tales

Adventures & experiences in contemporary technology

The dangers of overriding JavaScript onload event handlers

17th April 2008

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 as 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.

  • All the views that you find expressed on here in postings and articles are mine alone and not those of any organisation with which I have any association, through work or otherwise. As regards editorial policy, whatever appears here is entirely of my own choice and not that of any other person or organisation.

  • Please note that everything you find here is copyrighted material. The content may be available to read without charge and without advertising but it is not to be reproduced without attribution. As it happens, a number of the images are sourced from stock libraries like iStockPhoto so they certainly are not for abstraction.

  • With regards to any comments left on the site, I expect them to be civil in tone of voice and reserve the right to reject any that are either inappropriate or irrelevant. Comment review is subject to automated processing as well as manual inspection but whatever is said is the sole responsibility of the individual contributor.