当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript addLoadEvent函数说明

Javascript
Add a Table to a Word Document
Add Formatted Text to a Word Document
用jscript实现新建word文档
用jscript实现新建和保存一个word文档
Open and Print a Word Document
Use Word to Search for Files
Convert Seconds To Hours
Sample script that deletes a SQL Server database
Sample script that displays all of the users in a given SQL Server DB
firefox中用javascript实现鼠标位置的定位
div+css实现鼠标放上去,背景跟图片都会变化。
Locate a File Using a File Open Dialog Box
Save a File Using a File Save Dialog Box
用jscript实现列出安装的软件列表
List the Stored Procedures in a SQL Server database
Display SQL Server Login Mode
Display SQL Server Version Information
List all the Databases on a SQL Server
用jscript启动sqlserver
Stop SQL Server

Javascript 中的 javascript addLoadEvent函数说明


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 337 ::
收藏到网摘: n/a

网页加载完整后会触发一个onload事件,默认地一个事件只能和一个函数绑定。 为了在网页加载完后,同时执行多个函数,Simon Willison写了function addLoadEvent(func):
复制代码 代码如下:

function addLoadEvent(func){
var oldOnload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
}
else {
window.onload = function(){
oldOnload();
func();
}
}
}

addLoadEvent工作流程:
把现有的window.onload事件处理函数的值存入变量oldonload。
如果在这个处理函数上还没有绑定任何函数,就像平时那样把新函数添加给它;
如果在这个处理函数已经绑定了一些函数,就把函数追回到现有指令未尾。
浏览器加载html内容是自上而下的(默认),而JS一般是在哪里引入——想想如果JS里面包含了一些即时执行指令,
它会操作根本不存在元素节点(因为还没有加载完)会有什么后果?结果就是出错。
addLoadEvent可以实现无论有多少个函数,都能让它们同时和window.onload事件绑定。