Javascript 中的 window.onload 有 += 吗? [英] Is there += for window.onload in Javascript?

查看:21
本文介绍了Javascript 中的 window.onload 有 += 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我想到了以下问题:

recently I came up with the following problem:

在我网站的所有 html 页面中,我在 onLoad 事件中调用一个函数:

In my web site in all html pages I call a function in body onLoad event:

<body onLoad="func1();">

这是我的 html 模板的一部分,因此它出现在我网站的每个页面上,我无法更改它.现在,交易是在某些页面上,我需要调用一些其他函数 onload 并尝试使用 window.onload 属性,但它会清除 func1 的调用...

This is part of my template for html, so it appears on every page in my site and I can't change that. Now, the deal is that on some pages, I need to call some other functions onload and I tried with window.onload property, but it wipes the calling of func1...

我现在只能说:

window.onload = func2(); //where func2() calls to func1()

但这看起来又脏又蹩脚?不是吗?

but this seems dirty and lame? Isn't it ?

那么,有没有一种方法可以在不删除旧函数的情况下,为即将在加载时执行的函数添加一些函数?另外,如果有帮助的话,我会使用 asp.net ...

So, is there a way to add some functions to those that are about to be executed onload, without deleting the old one? In addition I use asp.net if that could help ...

谢谢!

推荐答案

您可以使用 jQuery 来链接负载处理程序.反复使用 jQuery.loadjQuery(document).ready 将链接您的处理程序(我相信).您的另一个选择是以编程方式进行,这意味着您需要一个辅助函数来为您链接您的 onload 处理程序.你可以用闭包(或匿名函数)来做到这一点:

You can use jQuery to chain on load handlers. Repeatedly using jQuery.load or jQuery(document).ready will chain your handlers (I believe). You other option is to do it programmatically, which means you need an auxiliary function that will chain your onload handlers for you. You can do this with a closure (or anonymous function):

var addOnLoadHandler = function(newHandler) {

    if (typeof window.onload != 'function') {
        window.onload = newHandler;
    }

    else {
        var oldHandler = window.onload;
        window.onload = function() {
            if (oldHandler) {
                oldHandler();
            }
            newHandler();
        };
    }
};

你必须以编程方式绑定你的函数,所以你必须这样做:

You will have to bind your functions programmatically though, so you would have to do:

addOnLoadHandlers(function() {
 alert("Hi I am the first onLoad handler!");
});

addOnLoadHandlers(function() {
 alert("Hi I am the second onLoad handler!");
});

在 javascript 文件中(或在您的 html 文件中).

in a javascript file (or in your html file).

另一种方法是使用数组:

Another approach is to use an array:

var onloaders = new Array();

function runOnLoads() {
    for (i = 0; i < onloaders.length; i++) {
        try {
            var handler = onloaders[i];
            handler();
        } catch(error) {
            alert(error.message);
        }
    }
}

function addLoader(obj) {
    onloaders[onloaders.length] = obj;
}

在您的 HTML 或 Javascript 文件中:

In your HTML or Javascript file you do:

addLoader(function() {
 alert("Hi I am the first onLoad handler!");
});

addLoader(function() {
 alert("Hi I am the second onLoad handler!");
});

然后在您的 html 中,您可以执行 <body onload="runOnLoads()">

Then in your html you can just do <body onload="runOnLoads()">

这篇关于Javascript 中的 window.onload 有 += 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆