Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:53:14+00:00 2026-06-13T10:53:14+00:00

I was told to use document.ready when I first started to use Javascript/jQuery but

  • 0

I was told to use document.ready when I first started to use Javascript/jQuery but I never really learned why.

Might someone provide some basic guidelines on when it makes sense to wrap javascript/jquery code inside jQuery’s document.ready?

Some topics I’m interested in:

  1. jQuery’s .on() method: I use the .on() method for AJAX quite a bit (typically on dynamically created DOM elements). Should the .on() click handlers always be inside document.ready?
  2. Performance: Is it more performant to keep various javascript/jQuery objects inside or outside document.ready (also, is the performance difference significant?)?
  3. Object scope: AJAX-loaded pages can’t access objects that were inside the prior page’s document.ready, correct? They can only access objects which were outside document.ready (i.e., truly “global” objects)?

Update: To follow a best practice, all my javascript (the jQuery library and my app’s code) is at the bottom of my HTML page and I’m using the defer attribute on the jQuery-containing scripts on my AJAX-loaded pages so that I can access the jQuery library on these pages.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:53:15+00:00Added an answer on June 13, 2026 at 10:53 am

    In simple words,

    $(document).ready is an event which fires up when document is
    ready.

    Suppose you have placed your jQuery code in head section and trying to access a dom element (an anchor, an img etc), you will not be able to access it because html is interpreted from top to bottom and your html elements are not present when your jQuery code runs.

    To overcome this problem, we place every jQuery/javascript code (which uses DOM) inside $(document).ready function which gets called when all the dom elements can be accessed.

    And this is the reason, when you place your jQuery code at the bottom (after all dom elements, just before </body>) , there is no need for $(document).ready

    There is no need to place on method inside $(document).ready only when you use on method on document because of the same reason I explained above.

        //No need to be put inside $(document).ready
        $(document).on('click','a',function () {
        })
    
        // Need to be put inside $(document).ready if placed inside <head></head>
        $('.container').on('click','a',function () {
        });
    

    EDIT

    From comments,

    1. $(document).ready does not wait for images or scripts. Thats the big difference between $(document).ready and $(document).load

    2. Only code that accesses the DOM should be in ready handler. If it’s a plugin, it shouldn’t be in the ready event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that, in jQuery, we are told to use $(document).ready() in order to
i have been told to use 'when' statement to make multiplexer but not use
I'm just wondering if someone could point out why one should use jQuery event
One of my friend told me Use font family: helvetica, arial, sans-serif in your
i'm writing an email template and was told to use only inline css (because
When I was using C++ in college, I was told to use multidimensional arrays
Someplace I saw a snippet of code which told vi to use soft tabs
I have previously been told that I should always use Randomize() before I use
I was told that the optimal way to program in C++ is to use
I'm trying to track podcast data. They told me I have to use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.