0

Please i need some recommendation, is it adviceable to use jQuery’s $(document).ready() because i had a problem loading external page using the load function, googled and stumbled on this page really want to know if i can run jquery script without $(document).ready() need your opinion.. Thank

  • 4
    Possible duplicate of [window.onload vs $(document).ready()](http://stackoverflow.com/questions/3698200/window-onload-vs-document-ready) – SamyQc Apr 08 '16 at 19:36
  • This will also be relevant for the time being: **[Dont initalize all the things in jQuery](http://elijahmanor.com/dont-initialize-all-the-things-in-jquery-ready/)** – AGE Apr 08 '16 at 19:39

4 Answers4

0

Since jQuery manipulates the document, using .ready makes sure your code runs when the document is ready, i.e. the browser has downloaded the HTML

The load will trigger later, when images are loaded, etc.

Also, place your <script> tag just before the <body> tags ends, this will speed up page loading and contribute to a better user experience.

Tudor Ilisoi
  • 2,699
  • 19
  • 21
  • Thank you, as much as i want to know – Marshall Unduemi Josiah Apr 08 '16 at 19:43
  • 2
    @MarshallUnduemiJosiah about the above answer: placing your ` – AGE Apr 08 '16 at 19:47
0

I think the best way to look at this is that no one thing is a fix-all for coding. The ready call is great but not for everything your JS does. Also, as others have suggested moving your JS before the body tag can help but not always.

Your question should not be should you use it but rather when to use it.

nerdlyist
  • 2,654
  • 2
  • 16
  • 26
  • Yeah course i found this [link](http://encosia.com/dont-let-jquerys-document-ready-slow-you-down/) Don’t let jQuery’s $(document).ready() slow you down – Marshall Unduemi Josiah Apr 08 '16 at 19:56
  • The article is good but again depends on your situation. The live examples are good but if you do not want the user going around trigger click events while the page loads then ready is better. The ajax call I feel is obvious (get the data asap) but also question if it may not impact load time in situations where it hangs or there is a lot to pull in. – nerdlyist Apr 08 '16 at 20:19
0

$document.ready() is not called in every partial postback. $document.ready() is called only one time i.e. during first time of page loading. if you want to get call back of every partial postback then you have to use pageLoad() method which is the part of scriptmanager generated script means if you want to use pageLoad() method then you need to use asp.net scriptmanager in the page.

Prabhat Sinha
  • 1,499
  • 17
  • 27
-1

JQuery is highly recommended for it cross-browser compatibility, community support, ease of use, and battle-tested.

$(document.ready()) is equivalent to:

document.addEventListener("DOMContentLoaded", function(event) {
    // code goes here

});

The window.load function or JQuery load is equivalent to

document.addEventListener("load", function(event) {
    // code goes here

});

The DOMContentLoaded fires when all of the HTML DOM has parsed. Load fires when all of the HTML DOM has parsed and images have finished loading

To use this method you must ensure you have included the JQuery library and it has loaded beforehand.