下面的例子演示了在页面载入完成时,执行自定义的函数:
<html>
<head>
<script type="text/javascript">
function load(){
alert("页面加载完成");
}
</script>
</head>
<body onload="load()">
</body>
</html>
第二个例子演示了在图像载入完成时,提示加载的图像名称:
<html>
<head>
<script type="text/javascript">
function load(x){
var s=x.src;
alert( "加载的图像为:" + s.substring(s.lastIndexOf("/")+1) );
}
</script>
</head>
<body>
<img src="http://www.520mg.com/Public/Images/article/logo_mark.gif" onload="load(this)" />
</body>
</html>
可以将第二个例子在实际应用中延伸一下,如图片下载完成后,取得图片尺寸并以最合适大小显示(reSize)等。