HTML事件屬性onload
當(dāng)加載元素時(shí)觸發(fā) onload
屬性事件。
onload
最常用于< body> 元件以在網(wǎng)頁已完全加載全部時(shí)執(zhí)行腳本內(nèi)容,包括圖像,腳本文件,CSS文件等。
我們也可以使用 onload
attribute event和iframe。
HTML5中的新功能
沒有。
句法
<element onload="script or Javascript function name">
<body>,
<frame>,
<frameset>,
<iframe>,
<img>,
<input type="image">,
<link>,
<script>
<style>
瀏覽器兼容性
|
|
|
|
|
|
onload |
Yes |
Yes |
Yes |
Yes |
Yes |
例子
以下代碼處理body元素上的onload事件。
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Page is loaded");
if (navigator.cookieEnabled == true) {
alert("Cookies are enabled.");
} else {
alert("Cookies are not enabled.");
}
}
</script>
</head>
<body onload="myFunction()">
<h1>Hello World!</h1>
</body>
</html>
Click to view the demo
實(shí)施例2
以下代碼處理圖像元素的onload事件。
<!DOCTYPE html>
<html>
<body>
<img src="/attachments/jimg/border.png" onload="loadImage()" width="100" height="100">
<script>
function loadImage() {
alert("Image is loaded");
}
</script>
</body>
</html>
Click to view the demo