添加多个window.onload事件 [英] Add multiple window.onload events

查看:506
本文介绍了添加多个window.onload事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET用户控件中,我将一些JavaScript添加到 window.onload 事件:

In my ASP.NET User Control I'm adding some JavaScript to the window.onload event:

if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), onloadScriptName))
  Page.ClientScript.RegisterStartupScript(this.GetType(), onloadScriptName, 
    "window.onload = function() {myFunction();};", true);            

我的问题是,如果已经存在 onload 事件,而不是覆盖它。如何在 onload 事件中允许两个用户控件执行JavaScript?

My problem is, if there is already something in the onload event, than this overwrites it. How would I go about allowing two user controls to each execute JavaScript in the onload event?

编辑:感谢第三方库的信息。我会记住它们。

Thanks for the info on third party libraries. I'll keep them in mind.

推荐答案

建议的大多数解决方案都是特定于Microsoft的,或者需要膨胀的库。这是一个好方法。这适用于符合W3C标准的浏览器和Microsoft IE。

Most of the "solutions" suggested are Microsoft-specific, or require bloated libraries. Here's one good way. This works with W3C-compliant browsers and with Microsoft IE.

if (window.addEventListener) // W3C standard
{
  window.addEventListener('load', myFunction, false); // NB **not** 'onload'
} 
else if (window.attachEvent) // Microsoft
{
  window.attachEvent('onload', myFunction);
}

这篇关于添加多个window.onload事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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