Add multiple [removed] events

前端 未结 8 1799
说谎
说谎 2020-12-02 10:31

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

if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType         


        
相关标签:
8条回答
  • 2020-12-02 11:17

    I had a similar problem today so I solved it having an index.js with the following:

    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:

    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!

    0 讨论(0)
  • 2020-12-02 11:18

    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);
    }
    
    0 讨论(0)
提交回复
热议问题