Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place $(document).ready(function()? [duplicate]

We often read here and there, that we must place our js code either on the page head section or before (sorry) the end body tag. Discussions about this aside, I'm just looking to know what are the reading order for such things by the browsers (taking that they do act as equals here):

Can we place:

$(document).ready(function(){

No matter where on the page structure, because we are using $(document).ready or should we still place it on the head section ?

Can anyone please clarify this.

If my question isn't clear, I can rephrase.

like image 762
MEM Avatar asked Jul 15 '13 16:07

MEM


People also ask

What does $( document .ready function () do?

$( document ).ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

Where do you put the document ready in HTML?

The function can be whatever you like - whatever you put inside the function will run when the document is ready (i.e. when the webpage is called by the browser). You don't need to insert it at the bottom of the HTML page - you can do it anywhere. People only insert it at the bottom to optimize their loading speed.

Can we have multiple document ready () function on the same page?

We can have multiple document. ready() function in our code but only one body. onload() is allowed.

What is difference between $( document .ready function () vs $( function ()?

The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.


2 Answers

You can place a script anywhere in the document. Best practice usually advises placing scripts in the footer for page load performance concerns. Further, best practice usually advises placing the scripts together for ease of maintenance.

However, per the spec, there is no restriction on where in the document you place a script tag. You may place them together in the header, at the bottom of the body, sprinkled all over the document, or any combination thereof.

The use of the jQuery construct $(document).ready has the same result regardless of where it is placed within the document. The key is to understand the functionality behind this construct:

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received.

So, ready is similar to document.onload, but not the same. It doesn't matter where the code is, if you execute it when document.onload is fired or when jQuery fires ready. Code's placement in a document is only significant if it is NOT wrapped by some event handler/listener.

The only restriction on the location on $(document).ready is that it cannot happen before you include the jQuery library. $(document).ready is using jQuery, so if jQuery doesn't exist.... you can't use it.

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
        <script>
            alert('executed as soon as it is reached (as the document is downloaded)');
            $(document).ready(function () { alert('on jQuery ready'); });
        </script>
    </head>
    <body onload="alert('executed on document.onload event');">
        <script>
            alert('executed as soon as it is reached (as the document is downloaded)');
            $(document).ready(function () { alert('on jQuery ready'); });
        </script>
    </body>
</html>

Documentation

  • SCRIPT specification at W3 - http://www.w3.org/TR/html401/interact/scripts.html
  • script (html 5) specification at W3 - http://www.w3.org/TR/html-markup/script.html
  • Placing Javascript in your pages at quirksmode - http://www.quirksmode.org/js/placejs.html
  • Jquery ready - http://api.jquery.com/ready/
like image 132
Chris Baker Avatar answered Sep 29 '22 10:09

Chris Baker


AFAIK, $(document).ready event gets raised after DOM is completely loaded so it doesn't matter where you place it.

But they say to write the script at end of the body because page will show up to the end user instantly and javascript will continue to run as background process.

like image 29
Sunny Avatar answered Sep 29 '22 09:09

Sunny