locked
Jquery with multiple $(document).ready .. RRS feed

  • Question

  • User1126057398 posted

    There is one aspx page with contains document.ready(). Moreover, few User Controls are used inside this aspx page, which further contains document.ready().  I am trying to shift all script data(of aspx  page and User Control) in single separate js file. How can these multiple document.ready() be fitted in a single file? 

    Saturday, November 23, 2019 8:21 AM

Answers

  • User1535942433 posted

    Hi geetasks,

    In my opinion,you could contains multiple $(document).ready() or have a lot of functions in one  $(document).ready().
    By the way,either will work just fine and work identically.However,a minor performance penalty will be added when using multiple callbacks.

    Solution1:
    Functions in multiple $(document).ready()  will fit your code layout more conveniently.
    More details ,you could refer to below code:

     <script src="Scripts/jquery-1.10.2.js"></script>
    
        <script>
        $(document).ready(function() {
            alert('Hello Tom!');
        });
            $(document).ready(function () {
                alert('Hello Jeff!');
            });
            $(document).ready(function () {
        alert('Hello Dexter!');
    });    
     </script>


    Solution2:
    functions in one $(document).ready(function() )  will result in more compact code.
    More details ,you could refer to below code:
     

    <script src="Scripts/jquery-1.10.2.js"></script>
    
        <script>
      $(document).ready(function() {
    
        alert('Hello Tom!');
        alert('Hello Jeff!');
        alert('Hello Dexter!');
     </script>
    


    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 27, 2019 9:34 AM

All replies

  • User475983607 posted

    How can these multiple document.ready() be fitted in a single file? 

    Any kind of rework depends on the current page and user control design.  The new file reference must be after the jQuery reference since $(document).ready is dependent on jQuery.

    Saturday, November 23, 2019 1:55 PM
  • User1535942433 posted

    Hi geetasks,

    In my opinion,you could contains multiple $(document).ready() or have a lot of functions in one  $(document).ready().
    By the way,either will work just fine and work identically.However,a minor performance penalty will be added when using multiple callbacks.

    Solution1:
    Functions in multiple $(document).ready()  will fit your code layout more conveniently.
    More details ,you could refer to below code:

     <script src="Scripts/jquery-1.10.2.js"></script>
    
        <script>
        $(document).ready(function() {
            alert('Hello Tom!');
        });
            $(document).ready(function () {
                alert('Hello Jeff!');
            });
            $(document).ready(function () {
        alert('Hello Dexter!');
    });    
     </script>


    Solution2:
    functions in one $(document).ready(function() )  will result in more compact code.
    More details ,you could refer to below code:
     

    <script src="Scripts/jquery-1.10.2.js"></script>
    
        <script>
      $(document).ready(function() {
    
        alert('Hello Tom!');
        alert('Hello Jeff!');
        alert('Hello Dexter!');
     </script>
    


    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 27, 2019 9:34 AM