Trigger event on body load complete js/jquery

前端 未结 10 1783
眼角桃花
眼角桃花 2020-12-05 18:36

I want to trigger one event on page load complete using javascript/jquery.

Is there any way to trigger event or call a simple function once page loading fully comple

相关标签:
10条回答
  • 2020-12-05 19:12

    This will call function when the DOM structure is ready

    $(document).ready(function () {
            userCommission();
            //Show commision box
            $('#userroles').change(function(){
               userCommission();
            });
    
            function userCommission() {
               var roles = $('#userroles').val();
               var result = roles.map(function (x) { 
                    return parseInt(x, 10); 
                });
                var i = result.indexOf(6);           
                console.log(i);
                if(i == -1) {
                    console.log('inside');
                    $('.user-commission :input').attr("disabled", true);;
                    $('#user-commission').hide();
                } 
            } });
    
    0 讨论(0)
  • 2020-12-05 19:14

    jQuery:

    $(function(){
      // your code...this will run when DOM is ready
    });
    

    If you want to run your code after all page resources including images/frames/DOM have loaded, you need to use load event:

    $(window).load(function(){
      // your code...
    });
    

    JavaScript:

    window.onload = function(){
      // your code...
    };
    
    0 讨论(0)
  • 2020-12-05 19:21

    When the page loads totally (dom, images, ...)

    $(window).load(function(){
        // full load
    });
    

    When DOM elements load (not necessary all images will be loaded)

    $(function(){
        // DOM Ready
    });
    

    Then you can trigger any event

    $("element").trigger("event");
    
    0 讨论(0)
  • 2020-12-05 19:24

    Everyone's mentioned the ready function (and its shortcuts), but even earlier than that, you can just put code in a script tag just before the closing body tag (this is what the YUI and Google Closure folks recommend), like this:

    <script type='text/javascript'>
    pageLoad();
    </script>
    </body>
    

    At this point, everything above that script tag is available in the DOM.

    So your options in order of occurrence:

    1. Earliest: Function call in script tag just before closing the body tag. The DOM is ready at this point (according to the Google Closure folks, and they should know; I've also tested it on a bunch of browsers).

    2. Earlyish: the jQuery.ready callback (and its shortcut forms).

    3. Late, after all page elements including images are fully loaded: window onload event.

    Here's a live example: http://jsbin.com/icazi4, relevant extract:

    </body>
    <script type='text/javascript'>
      runPage();
    
      jQuery(function() {
        display("From <tt>jQuery.ready</tt> callback.");
      });
    
      $(window).load(function() {
        display("From <tt>window.onload</tt> callback.");
      });
    
      function runPage() {
        display("From function call at end of <tt>body</tt> tag.");
      }
    
      function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = msg;
        document.body.appendChild(p);
      }
    </script>
    

    (Yes, I could have used jQuery for the display function, but I was starting with a non-jQuery template.)

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