添加多个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);
}