Is $(document).ready necessary?

JqueryDocument Ready

Jquery Problem Overview


I saw this question in stackoverflow but do not feel that it was answered at all.

Is $(document).ready necessary?

I link all my javascripts at the bottom of the page so in theory they are all running after the document is ready anyways.

Jquery Solutions


Solution 1 - Jquery

>Is $(document).ready necessary?

no

if you've placed all your scripts right before the </body> closing tag, you've done the exact same thing.

Additionally, if the script doesn't need to access the DOM, it won't matter where it's loaded beyond possible dependencies on other scripts.

For many CMS's, you don't have much choice of where the scripts get loaded, so it's good form for modular code to use the document.ready event. Do you really want to go back and debug old code if you reuse it elsewhere?

off-topic:

As a side note: you should use jQuery(function($){...}); instead of $(document).ready(function(){...}); as it forces the alias to $.

Solution 2 - Jquery

No, if your javascript is the last thing before close you won't need to add those tags.

As a side note, a shorthand for $(document).ready is the code below.

$(function() {
// do something on document ready
});

This question might be good. Did you read it? https://stackoverflow.com/questions/1438883/jquery-why-use-document-ready-if-external-js-at-bottom-of-page

Solution 3 - Jquery

No, it isn't necessary provided you know you do not have any deferred stuff happening-- and in most cases you will know if you have developed what you are working on from top to bottom.

--It is when you bring in someone else's code, without thoroughly auditing it, that you don't know.

So, ask yourself are you using a framework or editor that helps you with the structure? Are you bringing in someone else's code and you haven't bothered to read through each file? Are you prepared to go through the Operating System, Browser, and Browser Version matrix of testing your code? Do you need to squeeze every single ounce of speed from your code?

document.ready() makes many of those question become irrelevant. document.ready() was designed to make your life easier. It comes at a small (and I dare say acceptable) performance hit.

Solution 4 - Jquery

I have seen references/blog post across the internet regarding the usage of jquery's document.ready. In my opinion both using it or putting all your javascript at the bottom of the page are both valid. And now the question would be which would be better? It is just a matter of programming style.

Solution 5 - Jquery

No, it is not necessary. You can either put the script tag right before the body closing tag or if you are supporting IE9+ you can use native JS.

<script>alert('DOM Loaded!');</script>
</body>

<script>
document.addEventListener("DOMContentLoaded", function(event) { 
      // DOM has loaded ready to fire JS scripts
    });
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoshua RobisonView Question on Stackoverflow
Solution 1 - JqueryzzzzBovView Answer on Stackoverflow
Solution 2 - JqueryturtlepickView Answer on Stackoverflow
Solution 3 - JqueryBradChesney79View Answer on Stackoverflow
Solution 4 - JqueryKrishna ChytanyaView Answer on Stackoverflow
Solution 5 - JquerymbokilView Answer on Stackoverflow