Javascript 中有 window.onload 的 += 吗?

发布于 2024-08-22 04:29:56 字数 470 浏览 1 评论 0原文

最近我遇到了以下问题:

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

<body onLoad="func1();">

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

我现在只能说:

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

但这看起来很脏并且瘸?不是吗?

那么,有没有一种方法可以在那些即将执行onload的函数中添加一些函数,而不删除旧的函数呢?另外我使用asp.net,如果这可以帮助的话......

谢谢!

recently I came up with the following problem:

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

<body 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...

I now that I can just say:

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

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

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 ...

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

森林很绿却致人迷途 2024-08-29 04:29:56

您可以使用 jQuery 链接加载处理程序。重复使用 jQuery.load 或 jQuery(document).ready 会链接您的处理程序(我相信)。您的另一个选择是以编程方式执行此操作,这意味着您需要一个辅助函数来为您链接 onload 处理程序。您可以使用闭包(或匿名函数)来做到这一点:

var addOnLoadHandler = function(newHandler) {

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

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

不过,您必须以编程方式绑定您的函数,因此您必须这样做:

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

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

在 javascript 文件中(或在 html 文件中)。

另一种方法是使用数组:

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 文件中执行以下操作:

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

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

然后在 html 中执行

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!");
});

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;
}

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!");
});

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

苏璃陌 2024-08-29 04:29:56

您可能希望充分利用匿名函数:

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

借自 Top 10 自定义 JavaScript 函数所有时间

You may want to make the best out of anonymous functions:

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

Borrowed from the Top 10 custom JavaScript functions of all time.

好多鱼好多余 2024-08-29 04:29:56

jQuery 有一个很好的速记方法,可以将多个定义的处理程序添加到“ready”事件(不适用于必须使用 $(document).ready(function(){}); 的匿名函数)。

一个

$(myFunction);
$(myFunction2);

很大的优点是,如果 DOM 已经加载,它仍然会被触发,而在事件之后绑定到 window.onload 的任何内容都不会被调用。

jQuery has a nice shorthand for adding multiple defined handlers to the "ready" event (does not work with anonymous functions where you have to use $(document).ready(function(){});).

simply

$(myFunction);
$(myFunction2);

One big advantage is that if the DOM has already loaded, this still gets fired, whereas anything you bind to window.onload after the event will not get called.

黒涩兲箜 2024-08-29 04:29:56

你有没有考虑过像 jquery 这样的 javascript 库,我知道还有其他方法,但 jquery 会让你的生活变得更轻松......

$(function(){
   //Do stuff when DOM is loaded.
   func1();
    $('#link').click(function(){
        //bind a click event
    });
});

经典的方法是将所有函数放在页面底部:)

have you considered a javascript library like jquery, i know that there are other approaches but jquery will make your life so much easier...

$(function(){
   //Do stuff when DOM is loaded.
   func1();
    $('#link').click(function(){
        //bind a click event
    });
});

The classic approach is to just stick all of your functions at the bottom of the page :)

壹場煙雨 2024-08-29 04:29:56

在 jquery 中,您可以执行

$(document).onload(function() {
  // do something
}

//then 稍后执行

$(document).onload(function() {
  // do something here too!
}

jQuery 会智能地将这两个事件添加到 onload 事件中,并且这两个事件都将在页面加载时执行。使用 jQuery,您还可以获得跨浏览器支持作为额外的好处。

In jquery you can do

$(document).onload(function() {
  // do something
}

//then later on do

$(document).onload(function() {
  // do something here too!
}

jQuery will intelligently add both events to the onload event and both will be executed when the page loads. With jQuery you also get crossbrowser support as an added bonus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文