Best Jquery Declaration


/ Published in: jQuery
Save to your folder(s)

JQUERY READY EVENT BEST PRACTICE


Copy this code and paste it in your HTML
  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>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.