Javascript函数调用
如何在窗口的onload事件上调用多个javascript函数 例如Javascript函数调用,javascript,function-calls,Javascript,Function Calls,如何在窗口的onload事件上调用多个javascript函数 例如 window.onload=MyFunc(); //Single Function 但是,如果在窗口的onload事件中有多个函数要调用,该怎么办…将它们包装起来 window.onload = function() { MyFunc(); MyOtherFunc(); } 把它们包起来 window.onload = function() { MyFunc(); MyOtherFunc(); }
window.onload=MyFunc(); //Single Function
但是,如果在窗口的onload事件中有多个函数要调用,该怎么办…将它们包装起来
window.onload = function() { MyFunc(); MyOtherFunc(); }
把它们包起来
window.onload = function() { MyFunc(); MyOtherFunc(); }
或者可以将函数绑定到窗口的加载事件:
window.addEventListener("load", MyFunction, false);
window.addEventListener("load", MyOtherFunction, false);
对于IE legacy,您可能需要使用attachEvent方法:
window.attachEvent("load", MyFunction);
window.attachEvent("load", MyOtherFunction);
这种方法的问题是无法计算函数的执行顺序。或者您可以将函数绑定到窗口的加载事件:
window.addEventListener("load", MyFunction, false);
window.addEventListener("load", MyOtherFunction, false);
对于IE legacy,您可能需要使用attachEvent方法:
window.attachEvent("load", MyFunction);
window.attachEvent("load", MyOtherFunction);
这种方法的问题是,函数的执行顺序无法计算。为什么不采用一种老派的方法,在页面加载时调用一个函数内部有尽可能多的函数调用? window.addEventListener(“加载”,Foo,false) 其中Foo调用了所有必需的函数。
这样,如果需要,您可以控制订单和返回值 为什么不采用一种老式的方式,在页面加载时调用一个函数时,在其中调用尽可能多的函数? window.addEventListener(“加载”,Foo,false) 其中Foo调用了所有必需的函数。
这样,如果需要,您可以控制订单和返回值 下面是一个示例,可以解释:
// create an array that will contain all
// your functions that you would like to
// call on page load
var pageOnLoadEvents = [];
// Add one or more functions in pageOnLoadEvents array
pageOnLoadEvents.push(function() { alert("Hello!"); })
pageOnLoadEvents.push(function() { alert("How are you?"); })
// This will call when your page will load
window.onload = function() {
// Loop through each index of pageOnLoadEvents
for(var index = 0; index < pageOnLoadEvents.length; index++) {
// '()' brackets in the end tell them to make a call
// If you don't include '()' at the end this will
// suspect as a object as in JavaScript functions
// are first-class objects.
// document.write(pageOnLoadEvents[index].toString()); // treat as object
pageOnLoadEvents[index]();
}
}
addLoadEvent函数作为
参数是另一个应该
在页面加载后执行。
不同于直接分配给
window.onload,该函数添加
以任何
以前添加的onload函数将
先斩后奏
下面是一个示例,它将解释如何:
// create an array that will contain all
// your functions that you would like to
// call on page load
var pageOnLoadEvents = [];
// Add one or more functions in pageOnLoadEvents array
pageOnLoadEvents.push(function() { alert("Hello!"); })
pageOnLoadEvents.push(function() { alert("How are you?"); })
// This will call when your page will load
window.onload = function() {
// Loop through each index of pageOnLoadEvents
for(var index = 0; index < pageOnLoadEvents.length; index++) {
// '()' brackets in the end tell them to make a call
// If you don't include '()' at the end this will
// suspect as a object as in JavaScript functions
// are first-class objects.
// document.write(pageOnLoadEvents[index].toString()); // treat as object
pageOnLoadEvents[index]();
}
}
addLoadEvent函数作为
参数是另一个应该
在页面加载后执行。
不同于直接分配给
window.onload,该函数添加
以任何
以前添加的onload函数将
先斩后奏
这当然需要回退
attachEvent
IET上的代码这当然需要回退attachEvent
IET上的代码无论如何都是不正确的,除非MyFunc
返回函数对象。将括号放在MyFunc
之后意味着您将立即调用它,MyFunc
返回的值将分配给window.onload
。我想你的意思是window.onload=MyFunc
。无论如何,这是不正确的,除非MyFunc
返回函数对象。将括号放在MyFunc
之后意味着您将立即调用它,MyFunc
返回的值将分配给window.onload
。我想你的意思是window.onload=MyFunc
。