Can i use more than once $(document).ready() of jquery in a single html page?

programmer picture programmer · Aug 18, 2011 · Viewed 12k times · Source

i have one html page named "text.html"

<html>
    <body>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="js/Photos.js"></script>
        <script type="text/javascript" src="js/Animations.js"></script>
    </body>
</html>

Both Photos.js and Animations.js start with "$(document).ready()" like this

//Javascript File Photos.js
$(document).ready(function() {
    //My code here ....
});

//Javascript File Animations.js
$(document).ready(function() {
    //My code here ....
});

Does it matter if i use multiple $(document).ready(function() { in a single html page??

Thanks, in advance

Answer

wesbos picture wesbos · Aug 18, 2011

You can use as many as you would like, but its best to keep it to one for readability.

Also consider using short hand $(function() { ... });