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

  • SEARCH
  • Home
  • 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 7947391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:25:03+00:00 2026-06-04T01:25:03+00:00

The document.ready is used to execute code after the DOM is fully loaded. This

  • 0

The document.ready is used to execute code after the DOM is fully loaded. This can be used to attach event handlers to elements on the page e.g

$(function(){ 
    $('#somediv').click(function(){ 

    }); 
}) 

<div id="somediv"> </div> 

Internally, jQuery hooks up to DOMContentLoaded and window.onload as a fallback. In IE’s case an attempt is made to scroll the viewport over and over until successful.

I have a few questions, my first one being, when binding event handlers to the document itself, is it necessary to put that code in a document.ready ? I have always been writing the code below without wrapping it in a document.ready

$(document).keydown(function(e){
    if (e.which == 39) { 
       alert( "right arrow pressed" );
       return false;
    }
});

And as you can see, it works. My understanding is, since this code doesn’t hook up to any elements within the document, but the document itself, there’s no need to wrap it in a document.ready handler. Another reason i don’t wrap it is because i used to do the same in vanilla javascript the equivalent would be the code below, which also works.

document.onkeydown = function(){
var keyCode = event.keyCode || event.which;   
    if (keyCode == 39) { 
       alert( "right arrow pressed" );
       return false;
    }
}

I’ve seen numerous posts where people wrap it in a document.ready, is there any downside of not wrapping this code in document.ready ?

Also i think this question stems from my lack of clarity of what happens during this time when the DOM is being constructed, so if someone can explain what happens during the period right before the DOM is ready. To me the document is ready when the html has been parsed and converted into a DOM tree, or is there more to it ?

In summary, here are my questions

  1. When binding event handlers to the document itself, is it
    necessary to put that code in a document.ready.
  2. Are there any downsides to not wrapping the code in the document.ready ?
  3. What sequence of events take place when the document is being constructed, right before the document.ready is fired ?
  • 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-04T01:25:05+00:00Added an answer on June 4, 2026 at 1:25 am

    If you are binding to the document itself, you don’t need to wait until it is ready. There shouldn’t be any downsides to not wrapping it in document.ready in this case.

    document.ready gets fired when the DOMReady event is triggered by the browser, or when a specific test is successful for versions of browsers that don’t support the DOMReady event.

    Additional information. (5/22/12)

    Most modern browsers implement the DOMContentLoaded event which fires when all elements defined on the document are ready to be manipulated by javascript. Other browsers either rely on a setTimeout loop that continuously checks the readystate of the document or binds directly to the onreadystatechanged method of the document (taken from jquery core). The document itself is ready to be manipulated before javascript is ever executed, therefore you never need to wait when binding directly to the document.

    The only gotcha here is that if the code interacts with elements other than the document, there is a chance that the event could be triggered on the document before those elements exist. It is very unlikely for that to happen, but it can happen. If that is something that can happen with your code, then it makes sense to place it inside of $(document).ready() to prevent that scenario. Your sample doesn’t warrant being placed inside of $(document).ready().

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

Sidebar

Related Questions

I used this code for sending and returning result. <script type=text/javascript> $(document).ready(function() { $('.special').click(function(){
How document.ready event is implemented in jQuery. I mean whether they have used defer(which
I used $(document).ready(function() { $(#main ul li).hover(function(){ var fade = $('img.g-img', this); fade.fadeTo('slow', 0.5);
A while ago I used this simple function to hide/unhide divs ': $(document).ready(function() {
Before I heard about self executing functions I always used to do this: $(document).ready(function()
$(document).ready(function () { $(td:contains('1')).addClass('isOne'); }); I tried above code, but it also selects 11
$(document).ready(function() { $(span.link).mouseover(function(e){ $(this.children).css(display,inline); }); }); I'm not a javascript expert, but I've cobbled
$(document).ready(function() { $('#username').focus(function() { $(this).val(''); }) $('#username').blur(function() { var l = $(this).val.length; if (l
I have below script: $(document).ready(function () { $(#dpProject).change(function () { var idModel = $(this).val();
So for something like this: Jquery: $(document).ready(function(){ var container = $('.rotate_container'); var images =

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.