Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to call function 'before document ready' in Jquery?

I want to call function before document get ready, so is there any method in Jquery to do this?

like image 1000
user901886 Avatar asked Aug 25 '11 08:08

user901886


People also ask

Can I use jQuery without document ready?

It is in no way required. All it does is make sure that the DOM is loaded before trying to query for and use the elements inside the DOM (usually after dom is ready and before body. onload fires). You can use jquery perfectly fine without it.

How do you call a function in document ready jQuery?

$( document ). ready() ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ). on( "load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready. // A $( document ).

What happens before document ready?

The document ready event fired when the HTML document is loaded and the DOM is ready, even if all the graphics haven't loaded yet.

What is difference between $( function () and document Ready?

So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.


2 Answers

If you simply call a function in the head tag it will execute immediately, before the DOM is even parsed (that's the 'problem' that ready solves).

<!doctype html> <html>     <head>         <script>alert(1)</script>     </head>     <body>     </body> </html> 
like image 64
pimvdb Avatar answered Sep 21 '22 19:09

pimvdb


Just call your function before the document ready statement.

<script>    thisWillFireImmediately();     $(function() {       thisWillFireOnDocumentReady();    }); </script> 
like image 32
Scott Avatar answered Sep 23 '22 19:09

Scott