How to put a Javascript event on something?
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 "on开发者_如何转开发load = sdfsdf"
event. Then how?
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>
Use JQuery:
$(document).ready(function() {
});
http://docs.jquery.com/Tutorials:Introducing_%24%28document%29.ready%28%29
You can register events on the body through a (java)script using:
document.body.<event name> = function(){ // whatever your function does };
Use window.onload = function(){ ... }
. That has the equivalent result as <body onload="...">
.
put all your code in an function that autoexecute:
(function () {
//your code here
})();
and put it at the end of the body.
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
精彩评论