The dangers of overriding JavaScript onload event handlers

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.

Leave a Reply