How to do jquery code AFTER page loading?

前端 未结 8 1854
庸人自扰
庸人自扰 2020-12-22 19:07

If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the D

相关标签:
8条回答
  • 2020-12-22 19:30

    And if you want to run a second function after the first one finishes, see this stackoverflow answer.

    0 讨论(0)
  • 2020-12-22 19:32

    Use load instead of ready:

    $(document).load(function () {
     // code here
    });
    

    Update You need to use .on() since jQuery 1.8. (http://api.jquery.com/on/)

    $(window).on('load', function() {
     // code here
    });
    

    From this answer:

    According to http://blog.jquery.com/2016/06/09/jquery-3-0-final-released/:

    Removed deprecated event aliases

    .load, .unload, and .error, deprecated since jQuery 1.8, are no more. Use .on() to register listeners.

    https://github.com/jquery/jquery/issues/2286

    0 讨论(0)
  • 2020-12-22 19:33

    write the code that you want to be executed inside this. When your document is ready, this will be executed.

    $(document).ready(function() { 
    
    });
    
    0 讨论(0)
  • 2020-12-22 19:38

    You can avoid get undefined in '$' this way

    window.addEventListener("DOMContentLoaded", function(){
        // Your code
    });
    

    EDIT: Using 'DOMContentLoaded' is faster than just 'load' because load wait page fully loaded, imgs included... while DomContentLoaded waits just the structure

    0 讨论(0)
  • 2020-12-22 19:40

    I am looking for the same problem and here is what help me. Here is the jQuery version 3.1.0 and the load event is deprecated for use since jQuery version 1.8. The load event is removed from jQuery 3.0. Instead, you can use on method and bind the JavaScript load event:

    $(window).on('load', function () {
      alert("Window Loaded");
    });
    
    0 讨论(0)
  • 2020-12-22 19:50

    nobody mentioned this

    $(function() {
        // place your code
    });
    

    which is a shorthand function of

    $(document).ready(function() { .. });
    
    0 讨论(0)
提交回复
热议问题