javascript 多个javascript window.onload 解决方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16463998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple javascript window.onload solution
提问by Zhinto
Before i ask this question, i never post about questions like this but I don't understand how to implement it in my code. i have code like this
在我问这个问题之前,我从来没有发布过这样的问题,但我不明白如何在我的代码中实现它。我有这样的代码
window.onload = function() {
var url = getQueryVariable("url");
document.getElementById('view').src = url;
}
window.onload = function() {
var linkDirect = document.getElementsByClassName("frame");
for (var i = 0; i < linkDirect.length; i++) {
linkDirect[i].href = "http://namablog.blogspot.com/p/demo.html?url=" + linkDirect[i].href
}
}
then, how can I make the code execution using only one window.onload
那么,如何仅使用一个 window.onload 来执行代码
回答by Bart
You can use addEventListener
or any jQuery equivalent.
您可以使用addEventListener
或任何等效的 jQuery。
window.addEventListener('load', function (){
alert('Function #1');
});
window.addEventListener('load', function (){
alert('Function #2');
});
Be sure to call these beforethe window is loaded.
确保在加载窗口之前调用它们。
回答by rohit verma
window.addEventListener will not work in IE so use window.attachEvent
window.addEventListener 在 IE 中不起作用,所以使用 window.attachEvent
You can do something like this
你可以做这样的事情
function fun1(){
// do something
}
function fun2(){
// do something
}
var addFunctionOnWindowLoad = function(callback){
if(window.addEventListener){
window.addEventListener('load',callback,false);
}else{
window.attachEvent('onload',callback);
}
}
addFunctionOnWindowLoad(fun1);
addFunctionOnWindowLoad(fun2);
回答by SpYk3HH
Just my 2 cents, My personal fav way of doing this is as follows:
只是我的 2 美分,我个人最喜欢的方法如下:
function window_onload(cb) {
try {
if (typeof cb == 'function') {
if (document.readyState == 'complete') cb();
else if (window.hasOwnProperty('jQuery')) jQuery(window).on('load', cb);
else if (window['addEventListener']) document.addEventListener('load', cb, false);
else if (window['attachEvent']) {
// iFrame
if (window['frameElement']) document.attachEvent('onreadystatechange', function(){ if (document.readyState === 'complete') window_onload(cb); });
else window.attachEvent('onload', cb);
}
else {
var fn = window.onload; // very old browser, copy old onload
window.onload = function() { fn && fn(); ready(); };
}
}
}
catch (err) { if (window['console'] && console['error']) console.error("ERROR[window_onload()]", err); }
return window;
}
This pretty much covers just about everything. What little (mainly extremely old browsers I suppose?) it doesn't cover, you could easily debug, if needed. This also goes ahead and launches the callback if the document is already 'loaded'.
这几乎涵盖了所有内容。它没有涵盖什么(我想主要是非常旧的浏览器?),如果需要,您可以轻松调试。如果文档已经“加载”,这也会继续并启动回调。
回答by ZeeProgrammer
The simplest solution that has worked for me:
对我有用的最简单的解决方案:
function doOnLoad() {
onloadfn1();
onloadfn2();
onloadfn3();
}
window.onload = doOnLoad;
This article has explained it in details: http://developer.expressionz.in/blogs/2009/03/07/calling-multiple-windows-onload-functions-in-javascript/
这篇文章已经详细解释了:http: //developer.expressionz.in/blogs/2009/03/07/calling-multiple-windows-onload-functions-in-javascript/
I hope this helps some of you.
我希望这对你们中的一些人有所帮助。