views:

259

answers:

6

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

+1  A: 

Yeah, easy.. but how are you going to get the javascript in there? If you're already loading a single javascript library in all pages that allows you to hook in your method, you can use

window.onload = function() {};

and

window.onunload = function() {};

.. but if you have the option, I'd seriously suggest using a library like jQuery that hides all this muck from you.


Paul asked for clarification. Here's example code:

var hi = function() {
  alert('hi there.');
};

var bye = function() {
  alert('bye!');
};

if(window.addEventListener) {
  window.addEventListener('load', hi, false);
  window.addEventListener('unload', bye, false);
} else if(window.attachEvent) {
  window.attachEvent('load', hi);
  window.attachEvent('unload', bye);
}
Maas
Thanks for the reply! Could you suggest some details inside the function(){}? I'm really new to javascript...
Paul
@paul ummm... you're supposed to put your *code* there. Yes, *your code*.
Aviral Dasgupta
I don't know the API and that's my question...Well...maybe I've not yet got familiar to the rules here...Sorry.
Paul
Paul, you implied that you had a function or two in mind -- some reason to be adding onload / onunload / what-have-you events. I think from your comment you may simply be asking "can I dynamically add event listeners in javascript", and the answer is "yes". You can follow the model in the edited post above to add event listeners to any DOM node. You can find the correct node by using (e.g.) document.getElementById() to get the node you care about. Hope this helps.
Maas
@Mass, i am afraid it will not work. instead of `window.attachEvent('load', hi);` and `window.attachEvent('unload', bye);` you will have to use `window.attachEvent('onLoad', hi);` and `window.attachEvent('onUnload', bye);` Rest of the portion will work fine.
Rakesh Juyal
A: 

There are 2 solutions for this:

  1. You can use jQuery ready event
  2. Or you can use window.onload
Makram Saleh
$.ready =! onload cause it will be fired before onload. Btw there is one more way: window.addEventListener().
eskimoblood
A: 

Well, you could do one of these four things:

  • Use some scripting language like PHP, Python or ASP.Net to build a frontend to these pages, which adds in required code
  • Use tools like grep and sed to do a replace-all
  • Load mentioned pages in a frame , from a central 'Home page' and use DOM Manipulation to add said event listeners (although I believe this will not be possible for body.onLoad)
  • If all the pages already load some javascript, simply toss your even listener in there.
Aviral Dasgupta
+3  A: 

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:

</head>

And change it to

<script type="text/javascript" src="/global.js"></script>
</head>

Then at the root of your site create a file called global.js and put this in it:

window.onload = function() {
   // onload code here
}

window.onunload = function() {
   // onunload code here
}

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.

artlung
+1 for <head> <script type="text/javascript" src="/global.js"></script> </head>
Rakesh Juyal
Thanks Artlung. I just need add a couple of listeners and your suggestion helps all!
Paul
A: 

The normal way to add an event in firefox is obj.addEventListener( type, fn, false ). So the only thing to do is :

window.addEventListener('load', yourFunction, false );

There is no reason to use jquery for this.

eskimoblood
you will have to check if your browser supports `addEventListener` or not. `Everybody in this world is not using Firefox`. Use if ( window.addEventListener ) { //do FF } else if ( window.attachEvent ) { //do IE }.
Rakesh Juyal
He's looking for Firefox solution only.
eskimoblood
A: 

Since you only need this to work in Firefox, the DOMContentLoaded event (which means the HTML is loaded, not including images) should be listened to

document.addEventListener("DOMContentLoaded", function () {
    // your code here; document.body is available
}, false);
Eli Grey