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

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 javascript addLoadEvent函数说明


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 330 ::
收藏到网摘: 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事件绑定。