I’ve a Web site with hundreds of html pages that are already deployed. Now I want to add event listeners to body such as onload and onunload in all the html pages. I don’t want to manually change each of every of them, so I’m thinking whether I could dynamically add these listeners. My idea is to write javascript code that finds the DOM node of body and add the listeners to this node. Would it be possible? (it doesn’t have to be a cross-browser solution. the code is going to run on Firefox.)
Thanks!
Paul
If you want to add JavaScript you’ll end up needing to modify the site one way or another.
Possibly you’ll be doing a global search and replace and look for:
And change it to
Then at the root of your site create a file called global.js and put this in it:
What kind of things are you trying to do? The suggestion for jQuery or another library is a good one if you’re adding lots of event handlers, but if you merely need to add a few chunks of JS perhaps a library is overkill.