Add multiple [removed] events

前端 未结 8 1798
说谎
说谎 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:01

    Actually, according to this MSDN page, it looks like you can call this function multiple times to register multiple scripts. 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.

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

    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? If you've got two functions, call them both from a third script which you register for the event.

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

    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:

    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.

    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.

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

    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! Still doesn't seem to format code for me with IE7.

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

    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.

    For every *.js file I include I just wrap the code in a function.

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

    And there are also advantages of using domready instead of onload

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

    You can do this with jquery

    $(window).load(function () {
        // jQuery functions to initialize after the page has loaded.
    });
    
    0 讨论(0)
提交回复
热议问题