0

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 6

3

You could make a script to attach the event (i suppose that your problem is that you have no control over the html):

<script>
document.body.onload = function(){};
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Use JQuery:

$(document).ready(function() {

});

http://docs.jquery.com/Tutorials:Introducing_%24%28document%29.ready%28%29

Comments

1

You can register events on the body through a (java)script using:

document.body.<event name> = function(){ // whatever your function does };

3 Comments

Nitpicking, but it's on<event name>. The event name itself is without on.
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.
It doesn't matter much; just make sure you pass it without on in case you use addEventListener :)
0

Use window.onload = function(){ ... }. That has the equivalent result as <body onload="...">.

Comments

0

put all your code in an function that autoexecute:

(function () {
    //your code here
})();

and put it at the end of the body.

Comments

0

If you want to make everything by yourself, without jQuery. MDN addEventListener.

target.addEventListener(type, listener, useCapture<Optional>);

Depending on the required effect, listener:

  1. DOMContentLoaded - loading of the DOM
  2. load - loading of all the resource on the page - useful if the page contains large images

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.