I have a pre-made layout, cannot be changed. But I must put an onload() event to <BODY>. As I said, I'm not allowed just add "onload = sdfsdf" event. Then how?
6 Answers
Use JQuery:
$(document).ready(function() {
});
http://docs.jquery.com/Tutorials:Introducing_%24%28document%29.ready%28%29
Comments
You can register events on the body through a (java)script using:
document.body.<event name> = function(){ // whatever your function does };
3 Comments
pimvdb
Nitpicking, but it's
on<event name>. The event name itself is without on.Saket
well yes, if you read it that way :) ... but I've generally heard people associate the entire string "on..." as the event name. Anyway, quite a personal convention.
pimvdb
It doesn't matter much; just make sure you pass it without
on in case you use addEventListener :)If you want to make everything by yourself, without jQuery. MDN addEventListener.
target.addEventListener(type, listener, useCapture<Optional>);
Depending on the required effect, listener:
- DOMContentLoaded - loading of the DOM
- load - loading of all the resource on the page - useful if the page contains large images