What jQuery event is called right after $(document).ready()?

前端 未结 8 891
囚心锁ツ
囚心锁ツ 2021-01-13 08:47

I have lots of HTML generated on $(document).ready(). I have a simple window system. But not only it is generated on $(document).ready() - also som

相关标签:
8条回答
  • 2021-01-13 09:09

    By adding another handler function ones the document is ready, the handler will almost certainly be last one in the ready event queue. Effectively giving you an instant "post" ready handler function.

    $(document).ready(function() {
        $(document).ready(function() {
            // code to run "after" ready 
        });
    });
    

    $(document).ready

    When multiple functions are added via successive calls to this method, they run when the DOM is ready in the order in which they are added.

    Note that this will only work as long as no ones else uses this "trick".

    0 讨论(0)
  • 2021-01-13 09:14

    I usually don't advocate using setTimeout, but you can build on top of @jfriend00's answer to create a more abstract approach:

    $(document).ready(function() {
        setTimeout(function() {
            $(document).trigger('afterready');
        }, 1);
    });
    
    $(document).bind('afterready', function() {
        // call your code here that you want to run after all $(document).ready() calls have run
    });
    
    0 讨论(0)
  • 2021-01-13 09:15

    there is nothing after this function , so if you have some ajax loaders, only think you can do is to wait for all of them and then start rendering

    EDIT But i wonder why you dont just structuralize your code to eliminate this.

    0 讨论(0)
  • 2021-01-13 09:18

    $(document).ready() is called just after the DOM has finished loading. pageLoad() is called next on a 0 timer, but beware it is run after every partial postback.

    Edit: Added side note - this will only count if using ASP.NET, the pageLoad functionality mentioned is separate from jQuery. See more info Here

    0 讨论(0)
  • 2021-01-13 09:20

    There is another event which is fired later. it's $(window).load(); This is fired after all resources are loaded.

    But perhaps you want this:

    function loadWindowSystem(){
        // load window system here
    }
    
    $(document).ready(function(){
        // do some html stuff here
    
        loadWindowSystem();
    })
    

    This way you can separate your code in functions.

    0 讨论(0)
  • 2021-01-13 09:20

    You can use

    $(window).on('load', function () {
       alert("Window Loaded");
    });
    
    $(window).load(function(){
       alert("Window Loaded");
    }
    

    has been removed from jQuery.

    0 讨论(0)
提交回复
热议问题