[共同学习]贴段代码,大家来写执行结果,看谁理解更深刻,关于defer,attachEvent,window.onload....

Go_Rush 2006-04-19 01:01:43
<html>
<script>
window.attachEvent("onload",function(){alert('attach1')})
window.attachEvent("onload",function(){alert('attach2')})
</script>
<script>function window.onload(){alert('window.onload')}</script>
<script> alert('normal.1') </script>
<script defer> alert('defer1')</script>
<script defer> alert('defer2')</script>
<script> alert('normal.2') </script>

<SCRIPT event=onload for=window language=JavaScript>
alert('eventfor...')
</SCRIPT>

<body onload="javascript:alert('body onload=...')">
 
</body>
</html>

<script>alert('the last one')</script>


代码已经写得很清晰明了。
一个要求,先自己不要执行,看完代码立刻回贴写下上面代码的执行结果。
回贴后再检验自己的结果是否正确。 如果你写的结果不对,我们再聊聊。

上面代码最多最多可能弹出10个alert对话框。请把他们的弹出顺序依次写出来。

比如: 一定要在你调试之前写出来哦
attach1
attach2
window.onload
normal1
defer1
defer2
normal2
eventfor...
body onload=...
the last one...

如果你觉得某个语句不会弹出来,你就注明:(比如这样.)
defer2,normal2 不会出来


200分大贴,人人有分拿


上面的几乎是大部分常用的在页面加载完毕之后再执行script的代码
如果你能一次无调试的情况下写对的话,说明你很不错了
...全文
865 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
kx_o1986 2010-06-10
  • 打赏
  • 举报
回复
好贴,得仔细研究一下。
zzblue 2006-06-09
  • 打赏
  • 举报
回复
好贴
  • 打赏
  • 举报
回复
调用meizz的代码

<script>
if(!window.attachEvent && window.addEventListener)
{
Window.prototype.attachEvent = HTMLDocument.prototype.attachEvent=
HTMLElement.prototype.attachEvent=function(en, func, cancelBubble)
{
var cb = cancelBubble ? true : false;
this.addEventListener(en.toLowerCase().substr(2), func, cb);
};
}

window.attachEvent("onunload", function(){alert(88)});
</script>
这样就不用再写针对其它的浏览器兼容代码了
大猫钓鱼 2006-04-21
  • 打赏
  • 举报
回复
好帖!下班了,回去再细看。^_^
ttyp 2006-04-21
  • 打赏
  • 举报
回复
有兴趣的可以再加几个试试,果然似乎是没有什么规律
ttyp 2006-04-21
  • 打赏
  • 举报
回复
Binds the specified function to an event, so that the function gets called whenever the event fires on the object.

Syntax

bSuccess = object.attachEvent(sEvent, fpNotify)
Parameters

sEvent Required. String that specifies any of the standard DHTML Events.
fpNotify Required. Pointer that specifies the function to call when sEvent fires.

Return Value

Boolean. Returns one of the following possible values:

true The function was bound successfully to the event.
false The function was not bound to the event.

Remarks

When sEvent fires on the object, the object's sEvent handler is called before fpNotify , the specified function. If you attach multiple functions to the same event on the same object, the functions are called in random order, immediately after the object's event handler is called.


注意:这里说attachEvent调用函数时是随机顺序的,于是再加几个测试看看

<html>
<script>
window.attachEvent("onload",function(){alert('attach1')})
window.attachEvent("onload",function(){alert('attach2')})
window.attachEvent("onload",function(){alert('attach3')})
window.attachEvent("onload",function(){alert('attach4')})
</script>
<script>function window.onload(){alert('window.onload')}</script>
<script> alert('normal.1') </script>
<script defer> alert('defer1')</script>
<script defer> alert('defer2')</script>
<script defer> alert('defer3')</script>
<script> alert('normal.2') </script>

<SCRIPT event=onload for=window language=JavaScript>
alert('eventfor...')
</SCRIPT>

<body onload="javascript:alert('body onload=...')">
 
</body>
</html>

<script>alert('the last one')</script>

结果很奇怪,defer也是类似的,另外对于event的调用会覆盖onload的调用也感觉比较奇怪,总之这个帖子不错
Scarroot 2006-04-21
  • 打赏
  • 举报
回复
mark
ilvs 2006-04-20
  • 打赏
  • 举报
回复
晕,,我错的够多的...

<SCRIPT event=onload for=window language=JavaScript>
alert('eventfor...')
</SCRIPT>

这么多种方法都没见过.........我觉得还是用常用的方法比较好.像DW, script默认的标签参数里不会有这样的代码提示的.
iuhxq 2006-04-20
  • 打赏
  • 举报
回复
不过我确实不明白javascript加载和执行顺序
iuhxq 2006-04-20
  • 打赏
  • 举报
回复
学习研究还行,实际根本没人用这种代码来开发
nakuspenda 2006-04-20
  • 打赏
  • 举报
回复
只有'window.onload' 不会弹出...
ilvs 2006-04-20
  • 打赏
  • 举报
回复
出现,attach1,attach2,normal.1,normai.2,eventfor...,body onload=...,the last one



'window.onload' ,defer1,defer2,这三个不会出来.
xuzuning 2006-04-20
  • 打赏
  • 举报
回复
normal.1
normal.2
defer2
defer1
the last.one
evntfor
attach2
attach1
和我预想的基本一致

不过不太明白 eventfor...和body onload=...为什么不出现
虽说
<SCRIPT event=onload for=window language=JavaScript>
alert('eventfor...')
</SCRIPT>

<body onload="javascript:alert('body onload=...')">
应该有一个被覆盖了,但另一个也还是应该有的

  • 打赏
  • 举报
回复
to meizz(梅花雪)

<script>
if(!window.attachEvent && window.addEventListener)
{
Window.prototype.attachEvent = HTMLDocument.prototype.attachEvent=
HTMLElement.prototype.attachEvent=function(en, func, cancelBubble)
{
var cb = cancelBubble ? true : false;
this.addEventListener(en.toLowerCase().substr(2), func, cb);
};
}
</script>

这个代码该怎么用?比如要触发onunload事件。
  • 打赏
  • 举报
回复
to meizz(梅花雪)

为何下面的程序,没有任何反应呢?哪里错了?
<script>
if(!window.attachEvent && window.addEventListener)
{
Window.prototype.attachEvent = HTMLDocument.prototype.attachEvent=
HTMLElement.prototype.attachEvent=function(en, func, cancelBubble)
{
var cb = cancelBubble ? true : false;
this.addEventListener(en.toLowerCase().substr(2), func, cb);
};
}
function func(){alert('the last one');}
</script>
xxuu503 2006-04-20
  • 打赏
  • 举报
回复
俺错了
  • 打赏
  • 举报
回复
支持一下
xxuu503 2006-04-20
  • 打赏
  • 举报
回复
1 normal.1
2 normal.2
3 body onload=...
4 eventfor...
5 defer1
6 defer2
7 the last one
cvpc 2006-04-20
  • 打赏
  • 举报
回复
好帖子!
Go_Rush 2006-04-20
  • 打赏
  • 举报
回复
再顶一下,午饭后结贴
加载更多回复(16)

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧