转自:http://www.xiaoxiaozi.com/2010/01/05/1683/
在判断 iframe 是否加载完成的完美方法一文中,怿飞最后有如下两点补充:
- IE 支持 iframe 的 onload 事件,不过是隐形的,需要通过 attachEvent 来注册。
- 第二种方法比第一种方法更完美(采用readystatechange判断),因为 readystatechange 事件相对于 load 事件有一些潜在的问题。
这里感觉说的并不是完全准确,开始给我造成了很大的困扰。看其代码才明白,真正意义上来讲IE在创建一个新的iframe时的onload方法需要使用attachEvent来绑定,而原来就存在的iframe的onload方法,则可以直接绑定。
说的有些乱,大家看代码,一看便知:
<iframe id
=
'google'
></iframe
>
<script type = 'text/javascript' >
document. getElementById ( 'google' ). src = 'http://g.cn/' ;
document. getElementById ( 'google' ). onload = function ( ) {
alert ( 'I am google frame, now loaded' ) ;
}
</script >
<script type = 'text/javascript' >
document. getElementById ( 'google' ). src = 'http://g.cn/' ;
document. getElementById ( 'google' ). onload = function ( ) {
alert ( 'I am google frame, now loaded' ) ;
}
</script >
在这里,也把原文提到的”判断 iframe 是否加载完成的完美方法”原文摘录至此
var iframe
= document.
createElement
(
"iframe"
)
;
iframe. src = "http://www.planabc.net" ;
if (iframe. attachEvent ) {
iframe. attachEvent ( "onload" , function ( ) {
alert ( "Local iframe is now loaded." ) ;
} ) ;
} else {
iframe. onload = function ( ) {
alert ( "Local iframe is now loaded." ) ;
} ;
}
document. body. appendChild (iframe
iframe. src = "http://www.planabc.net" ;
if (iframe. attachEvent ) {
iframe. attachEvent ( "onload" , function ( ) {
alert ( "Local iframe is now loaded." ) ;
} ) ;
} else {
iframe. onload = function ( ) {
alert ( "Local iframe is now loaded." ) ;
} ;
}
document. body. appendChild (iframe