- window.onload = function() {
- init();
- doSomethingElse();
- };
- <!doctype html>
- <html>
- <head>
- <title>onload test</title>
- <script>
- function load() {
- alert("load event detected!");
- }
- window.onload = load;
- </script>
- </head>
- <body>
- <p>The load event fires when the document has finished loading!</p>
- </body>
- </html>
The load
event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.
There are also Gecko-Specific DOM Events like DOMContentLoaded
and DOMFrameContentLoaded
(which can be handled using element.addEventListener()
) which are fired after the DOM for the page has been constructed, but do not wait for other resources to finish loading.
以上引用:https://developer.mozilla.org/en/DOM/window.onload
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>addEventListener增加事件监听</title>
<style type="text/css">
.t{border: 1px solid red;width:100%;border-collapse:collapse;border-sapcing:0px;}
#t1{background-color:pink;}
</style>
<script type="text/javascript">
function modifyText () {
// Function to change the content of t2
var t2 = document.getElementById("t2");
t2.firstChild.nodeValue = "three";
}
function load () {
// Function to add event listener to t
var el = document.getElementById("t");
el.addEventListener("click",modifyText,false);
}
document.addEventListener("DOMContentLoaded",load,false);
</script>
</head>
<body>
<table class="t" id="t">
<tr>
<td id="t1">one</td>
</tr>
<tr>
<td id="t2">two</td>
</tr>
</table>
</body>
</html>
onload等所有的资源加载完调用