jQuery tip – skip $(document).ready() event

If you are new to jQuery you will notice that almost everything is wrapped with

           //your fancy javascript code here...

What does it do? It check if all HTML tags are loaded and if so, execute your code. Easy as that. But if you are new to jQuery it’s really strange way of writing JS code :).

Did you know that this is not so required?

Actually if you move your JavaScript code at the end of the page you don’t need to wrap your code with that.

Hope this will be handy for someone.

4 thoughts on “jQuery tip – skip $(document).ready() event

  1. Bisko

    And if you happen to use IE6, then if you do any dom manipulation (adding elements, moving them, etc) to the document.body, then you will have the nice Cannot open page error 🙂

  2. Vasilis

    Well, IE6 (an other old browser versions) cannot be taken into consideration, otherwise CSS3 and HTML5 shouldn’t be used. 🙂

  3. Toby

    It isn’t a strange way of writing jQuery, it is the correct way.

    It doesn’t mean that HTML tags are loaded, it means that the DOM is ready to be manipulated, these are two different things.

    If you include this at the end of your document to run automatically, even if you put it at the very end before the closing body tag you could potentially be manipulating a DOM without a closing body or html tag.

    It is a fairly straightforward concept, if someone doesn’t get it then they should learn, not just remove it.

Leave a Reply

Your email address will not be published. Required fields are marked *