Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.ready inside body tag

I have a web app that has a universal HTML header include, so I'd like to put page-specific scripts in each page instead of having it load for everything. I tried putting document.ready() near the end of the <body> of a page and it seems to be working fine. Are there any potential hazards to putting it there instead of inside the <head>?

like image 616
Matthew Avatar asked Feb 03 '09 23:02

Matthew


People also ask

Where do you put document ready?

So technically it doesn't matter where you put it. Many people like putting script in the head, because it makes sure the script is read before the page is loaded. Other people like putting it at the very end (just before the end body tag) so that all of the elements of the page are loaded before the script reads them.

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.

What is $( document .ready function ()?

$( 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.

Can we have two document ready in a Web page?

Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.


2 Answers

Yahoo recommends putting all scripts at the end of your document for performance - http://developer.yahoo.com/performance/rules.html

like image 134
Dan Avatar answered Sep 22 '22 16:09

Dan


None. Just make sure everything is loaded in the proper order.

like image 27
Eric Wendelin Avatar answered Sep 20 '22 16:09

Eric Wendelin