在chrome中window.onload事件的一些问题

在写一些关于图片操作的代码的时候,一般都需要在图片加载完成之后再执行程序。然而在Chorme中(貌似Safari也是)对window.onload的理解与IE和FF有偏差。

假如我们有一个如下的页面:

复制代码 代码如下:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<img src="https://www.jb51.net/upload/2010-3/20100301192859481.gif" alt="">
<script type="text/javascript">
var init=function (){
var img=document.getElementById('image');
alert(img.offsetWidth);
}
window.onload=new init();
</script>
</body>
</html>


在IE和FF下运行的时候都是可以显示图片的真实大小的,即使我并没有显示的说明这个img的width和height。但是在chrome下,则显示的是0。
但是如果将window.onload后面的new init()改为
window.onload=init
或者
window.onload=function(){new init(){}}
就可以在图片加载之后读出图片的大小。
转载请保留以下信息
作者:北玉(tw:@rehawk)

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdsszp.html