Posted By

SmellWing on 09/27/15


Tagged

jquery ready


Versions (?)

Best Jquery Declaration


 / Published in: jQuery
 

URL: http://gregfranko.com/jquery-best-practices/#/8

JQUERY READY EVENT BEST PRACTICE

  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  2.  
  3. <script>
  4. // IIFE - Immediately Invoked Function Expression
  5. (function(yourcode) {
  6.  
  7. // The global jQuery object is passed as a parameter
  8. yourcode(window.jQuery, window, document);
  9.  
  10. }(function($, window, document) {
  11.  
  12. // The $ is now locally scoped
  13.  
  14. // Listen for the jQuery ready event on the document
  15. $(function() {
  16.  
  17. console.log('The DOM is ready');
  18.  
  19. // The DOM is ready!
  20.  
  21. });
  22.  
  23. console.log('The DOM may not be ready');
  24.  
  25. // The rest of code goes here!
  26.  
  27. }));
  28. </script>

Report this snippet  

You need to login to post a comment.