Skip to content

Instantly share code, notes, and snippets.

@blackDelta
Last active August 29, 2015 13:56
Show Gist options
  • Save blackDelta/9323850 to your computer and use it in GitHub Desktop.
Save blackDelta/9323850 to your computer and use it in GitHub Desktop.
5 DIFFERENT TYPES OF DOCUMENT READY EXAMPLES
$(document).ready(function() {
//do jQuery stuff when DOM is ready
});
$(function(){
//jQuery code here
});
jQuery(document).ready(function($) {
    //do jQuery stuff when DOM is ready
});
jQuery.noConflict(); // Reverts '$' variable back to other JS libraries
jQuery(document).ready( function(){
         //do jQuery stuff when DOM is ready with no conflicts
   });  
//or the self executing function way
 jQuery.noConflict();
 (function($) {
    // code using $ as alias to jQuery
})(jQuery);
(function($) {
    // code using $ as alias to jQuery
  $(function() {
    // more code using $ as alias to jQuery
  });
})(jQuery);
// other code using $ as an alias to the other library
$(window).load(function(){  
    //initialize after images are loaded  
});
/*
Adding the jQuery can help prevent conflicts with other JS frameworks.
Why do conflicts happen?
Conflicts typically happen because many JavaScript Libraries/Frameworks use the same shortcut
name which is the dollar symbol $. Then if they have the same named functions the browser gets
confused!
How do we prevent conflicts?
Well, to prevent conflicts i recommend aliasing the jQuery namespace (ie by using example 3 above).
Then when you call $.noConflict() to avoid namespace difficulties (as the $ shortcut is no longer available)
we are forcing it to wrtie jQuery each time it is required.
[Refference]
- http://www.jquery4u.com/dom-modification/types-document-ready/
*/
@blackDelta
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment