【发布时间】:2011-03-25 18:22:10
【问题描述】:
我有一个在页面加载时启动的脚本,我一直在使用下面的代码来启动它:
if (window.addEventListener) {
window.addEventListener('load', otherRelatedParts, false);
}
else if (window.attachEvent) {
window.attachEvent('onload', otherRelatedParts );
}
但今天我尝试了这样的自调用函数:
(function() {
otherRelatedParts();
}())
它似乎在所有浏览器中都有效,而且代码更少。这是向窗口加载添加事件的首选方式吗?
【问题讨论】:
-
不需要自调用功能,只需
otherRelatedParts()即可。
标签: javascript javascript-events