简体   繁体   English

添加多个 window.onload 事件

[英]Add multiple window.onload events

In my ASP.NET User Control I'm adding some JavaScript to the window.onload event:在我的 ASP.NET 用户控件中,我向window.onload事件添加了一些 JavaScript:

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

My problem is, if there is already something in the onload event, than this overwrites it.我的问题是,如果onload事件中已经存在某些内容,则会覆盖它。 How would I go about allowing two user controls to each execute JavaScript in the onload event?我将如何允许两个用户控件在onload事件中分别执行 JavaScript?

Edit: Thanks for the info on third party libraries.编辑:感谢有关第三方库的信息。 I'll keep them in mind.我会记住他们的。

Most of the "solutions" suggested are Microsoft-specific, or require bloated libraries. 建议的大多数“解决方案”都是特定于Microsoft的,或者需要膨胀的库。 Here's one good way. 这是一个好方法。 This works with W3C-compliant browsers and with Microsoft IE. 这适用于符合W3C标准的浏览器和Microsoft IE。

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

There still is an ugly solution (which is far inferior to using a framework or addEventListener / attachEvent ) that is to save the current onload event: 还有一个丑陋的解决方案(远远不如使用框架或addEventListener / attachEvent )来保存当前的onload事件:

function addOnLoad(fn)
{ 
   var old = window.onload;
   window.onload = function()
   {
       old();
       fn();
   };
}

addOnLoad(function()
{
   // your code here
});
addOnLoad(function()
{
   // your code here
});
addOnLoad(function()
{
   // your code here
});

Note that frameworks like jQuery will provide a way to execute code when the DOM is ready and not when the page loads. 请注意,像jQuery这样的框架将提供一种在DOM准备就绪时执行代码的方法,而不是在页面加载时执行代码。

DOM being ready means that your HTML has loaded but not external components like images or stylesheets, allowing you to be called long before the load event fires. DOM已准备好意味着您的HTML已加载但不包含外部组件(如图像或样式表),允许您在加载事件触发之前调用很长时间。

I had a similar problem today so I solved it having an index.js with the following: 我今天遇到了类似的问题所以我解决了它有一个index.js与以下内容:

window.onloadFuncs = [];

window.onload = function()
{
 for(var i in this.onloadFuncs)
 {
  this.onloadFuncs[i]();
 }
}

and in additional js files that i want to attach the onload event I just have to do this: 在我要附加onload事件的其他js文件中,我只需要这样做:

window.onloadFuncs.push(function(){
 // code here
});

I normally use jQuery though, but this time I was restricted to pure js wich forced to use my mind for a while! 我通常使用jQuery,但这次我被限制为纯粹的js,被迫使用我的思想一段时间!

Mootools is another great JavaScript framework which is fairly easy to use, and like RedWolves said with jQuery you can can just keep chucking as many handlers as you want. Mootools是另一个非常好用的JavaScript框架,就像RedWolves所说的jQuery一样,你可以随意使用多个处理程序。

For every *.js file I include I just wrap the code in a function. 对于我包含的每个* .js文件,我只需将代码包装在函数中。

window.addEvent('domready', function(){
    alert('Just put all your code here');
});

And there are also advantages of using domready instead of onload 使用domready而不是onload也有优势

Try this: 试试这个:

window.attachEvent("onload", myOtherFunctionToCall);

function myOtherFunctionToCall() {
    // do something
}

edit: hey, I was just getting ready to log in with Firefox and reformat this myself! 编辑:嘿,我刚刚准备好登录Firefox并自行重新格式化! Still doesn't seem to format code for me with IE7. 似乎仍然没有为IE7格式化代码。

You can do this with jquery 你可以用jquery做到这一点

$(window).load(function () {
    // jQuery functions to initialize after the page has loaded.
});

I don't know a lot about ASP.NET, but why not write a custom function for the onload event that in turn calls both functions for you? 我不太了解ASP.NET,但为什么不为onload事件编写自定义函数,而后者又为你调用这两个函数? If you've got two functions, call them both from a third script which you register for the event. 如果您有两个函数,请从注册该事件的第三个脚本中调用它们。

您是否考虑过使用类似JQuery的东西来提供清理添加多个事件处理程序的框架?

Actually, according to this MSDN page , it looks like you can call this function multiple times to register multiple scripts. 实际上,根据这个MSDN页面 ,看起来你可以多次调用这个函数来注册多个脚本。 You just need to use different keys (the second argument). 您只需要使用不同的键(第二个参数)。

Page.ClientScript.RegisterStartupScript(
    this.GetType(), key1, function1, true);

Page.ClientScript.RegisterStartupScript(
    this.GetType(), key2, function2, true);

I believe that should work. 我认为应该有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM