Why is $(document).ready not firing for me?

后端 未结 12 1604
后悔当初
后悔当初 2020-12-10 00:26

In a php file i have used include to include the following js.php file and prior to that i have included the jquery file.



        
相关标签:
12条回答
  • 2020-12-10 01:04

    instead of $( document ).ready( function() { ... } );

    try

    $( document ).ready( abc() );

    function abc() { .... }

    worked for me.i myself searched lot of places, but couldn't find a solution. Tried this out randomly and worked!!

    0 讨论(0)
  • 2020-12-10 01:10

    In my case document.ready was not called because a html form was sent to server by Ajax request and only part of document was recived - and document ready is not called after ajax request.

    In consequence some buttons which were on that part that was reloaded was not binded to events .click() atc.. anymore because these buttons were new and binding of these events was called during document ready only first time - not for AJAX.

    0 讨论(0)
  • 2020-12-10 01:10

    I faced a similar problem too and this is what I did.

    Ensure that document.ready is placed after the callback function is defined in your javascript file.

    Here, init is not defined yet when document.ready is being parsed.

    //won't work
    $(document).ready(init);
    var init = function() {
       console.log('init not called');
    }
    

    Correct method:

    //Works
    var init = function() {
       console.log('document is ready');
    }
    $(document).ready(init);
    

    Notice that $(document).ready(init) is placed at the end.

    0 讨论(0)
  • 2020-12-10 01:10

    Following Zakas' The art of throwing JavaScript errors, I began to throw my own errors in JavaScript as an approach to finding JS issues.

    But this approach unfortunately started causing the jQuery ready() functions to silently fail, for unrelated parts of the site. :(

    I learned that the following are different:

    throw new Error()
    

    is different from

    console.error()
    

    So, I started to use console.error(), and I still got the red highlighting in the console.

    0 讨论(0)
  • 2020-12-10 01:11
    1. Check whether jQuery is loaded correctly.
    2. Look at the browser's progress bar: it may be loading some counters and the document is not ready until they're loaded: this often happens when external resources are slow.
    3. Try $(function(){ alert(...); }); just in case...
    4. Check whether you have JS errors prior to this onload binding. Use Firefox's FireBug plugin to check it out.
    0 讨论(0)
  • 2020-12-10 01:14

    The most likely answer, based on what you have said, is that the core jQuery file is not actually included correctly in the page. You need something like:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    

    Chances are, this is missing or typed incorrectly.

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