jQuery Plugin Template


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



Copy this code and paste it in your HTML
  1. ( function( $ ) {
  2.  
  3. // Global variables
  4. var i, j;
  5.  
  6. // Default settings
  7.  
  8. var settings = {
  9. "property" : 'value'
  10. };
  11.  
  12. // Methods
  13.  
  14. var methods = {
  15.  
  16. init: function(options) {
  17.  
  18. // Capture $(this) global as a local variable
  19. var $this = $(this);
  20.  
  21. // Extend settings with provided options
  22. if( options ) {
  23. $.extend(settings, options);
  24. }
  25.  
  26. // Example method call
  27. $this.pluginName('methodTwo');
  28.  
  29. $this.pluginName('methodThree');
  30.  
  31. },
  32.  
  33. methodTwo: function() {
  34.  
  35. },
  36.  
  37. methodThree: function() {
  38.  
  39. }
  40.  
  41. };
  42.  
  43. // Plugin namespace initialization
  44.  
  45. $.fn.pluginName = function(methd) {
  46.  
  47. if( methods[method] ) {
  48. return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
  49. } else if (typeof method === 'object' || !method ) {
  50. return methods.init.apply( this, arguments );
  51. } else {
  52. $.error( 'Method ' + method + ' does not exist on jQuery.pluginName' );
  53. }
  54.  
  55. }
  56.  
  57. // Standalone functions
  58.  
  59. function doThis(property) {
  60.  
  61. return 1;
  62.  
  63. }
  64.  
  65. } (jQuery) );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.