添加多个 window.onload 事件 [英] Add multiple window.onload events
问题描述
在我的 ASP.NET 用户控件中,我向 window.onload
事件添加了一些 JavaScript:
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屋!