0

I have been looking around a bit and haven't really been able to find something that says when is best to use these document ready types. The main one i use is $(document).ready(function() { because the syntax is clean and easy to read for me but is there an advantage of using this or not using this over the following?

Example 1:

$(document).ready(function() {

Example 2:

(function($) { 
    // code here
  $(function() {
    // code here
  });
})(jQuery);

Example 3:

$(window).load(function(){  
     //code here 
}); 

Example 4:

jQuery(document).ready( function(){

Example 5:

jQuery(document).ready(function($) {
    //code here
});

Example 6:

$(function(){

I know there are a few questions on here about this but mine is asking for examples of when to actually use these and how it will benefit from the others to use it?

Epik
  • 3,327
  • 4
  • 17
  • 23
  • 3
    jQuery API docs would be good place to look maybe? http://api.jquery.com/ready/ `$` is simply an alias for `jQuery` – charlietfl Jan 28 '15 at 01:11
  • 4
    1,2,4,5,6 are all the same (with respect to when the `// code here` is executed), just with different variable names and/or shortcuts. – Felix Kling Jan 28 '15 at 01:13
  • 1
    They all do the same thing. Example 2 is redundant unless there exists a variable `$` in the scope that you called it. `$` by default is just a shorthand for the global object `jQuery`. window load and document ready however are different **events**. – Austin Brunkhorst Jan 28 '15 at 01:14
  • @AustinBrunkhorst _3_ is `load` and not `ready` – t.niese Jan 28 '15 at 01:15
  • Yes, i looked at that. It explains them vaguely but does not say when it is best to use them against using the others. My question was "when to actually use these and how it will benefit from the others"? – Epik Jan 28 '15 at 01:16
  • I understant that $ is just shorthand for calling jQuery but is there any advantage of using say jQuery(document).ready( function(){ over $(document).ready( function(){. Would there ever be a need to use jQuery(document).ready( function(){ or a situation that i would need to use this in? I can get around fine in most cases using $(document).ready( function(){ but i don't know if i could be using one of the others in situations to IMPROVE my coding skills? – Epik Jan 28 '15 at 01:19
  • 2
    @Epik would use `jQuery` in `noConflict()` situations. There is no other benefit...is the same function when `$` is available. – charlietfl Jan 28 '15 at 01:21

2 Answers2

1

To understand ready(), you need to know about DOMContentLoaded event.

The DOMContentLoaded event is fired when the document has been completely downloaded and parsed, all the external scripts and internal scripts are downloaded and executed and DOM is ready to start manipulating. This event will not wait for stylesheets, images, and subframes to finish loading. There's a load event which can be used to detect a fully-loaded page.

As you can see here DomContentLoaded event is supported by almost all browsers except below < IE9. There're events like readystatechange which can be used to emulate DOMContentLoaded in IE8 and in IE7, we need to call doScroll() and see if throws an exception, it will not throw any exception once DOM is loaded.

Instead of you worrying about these cross-browser differences, ready() in jQuery abstracts the functionality to pass callback to invoke on DOMContentLoaded event.

DOMContentLoaded event started at FF and later adopted by all main stream browsers like Chrome, Opera and IE but standardised in HTML5. This event will fire only in case of documents where as load will be triggered on successful loading of any resource like <script> <img>. Now know that we know that there's a gap between DOMContentLoaded and load events, many people use this event's callback to do some javascript initilization while other resources are being loaded.

You can even see these events with tools like DeveloperTools in Chrome

browser events

Example 2 and Example 1 are both same in terms of their functionality-used to pass a callback for DOMContentLoaded, except that in 2nd one, you're passing jQuery as argument but reading it as $, so all the functions inside that function expression can work with $ and you can still have some other library outside using $ as global object with its own functions, that way conflict between two libraries using same $ as global object is resolved.

Example 1 & 4 are same but with minor difference of using $ or jQuery as global object, some other library might be using $ for their code but it's highly unlikely that jQuery is used as global object in some other library (For Example, AngularJS has its own global object angular )

Example 5 is again using jQuery on the outside and $ in the function callback. So once again namespace conflicts with $ are resolved.

Hope this gives some insight :)

Arkantos
  • 6,530
  • 2
  • 16
  • 36
0

All are exactly the same apart from 3.

From jQuery - What are differences between $(document).ready and $(window).load?

$(document).ready(function() {
 // executes when HTML-Document is loaded and DOM is ready
 alert("document is ready");
});


$(window).load(function() {
 // executes when complete page is fully loaded, including all frames, objects and images
 alert("window is loaded");
});
Community
  • 1
  • 1
Ben Aston
  • 53,718
  • 65
  • 205
  • 331