SlideShare a Scribd company logo
Building Robust jQuery Plugins
Why bother?
$.each({ focus: 'focusin', blur: 'focusout' }, function( original, fix ){ $.event.special[fix] = { setup:function() { if ( $.browser.msie ) return false; this.addEventListener( original,  $.event.special[fix].handler, true ); }, teardown:function() { if ( $.browser.msie ) return false; this.removeEventListener( original, $.event.special[fix].handler, true ); }, handler: function(e) { arguments[0] = $.event.fix(e); arguments[0].type = fix; return $.event.handle.apply(this, arguments); } }; });
Don't reinvent the wheel
“  Given enough eyeballs, all bugs are shallow  ” -- Eric S. Raymond
Infrastructure
Design
Consider the audience
 
 
 
jQuery.validator. addMethod ("domain", function(value) { return /^http://mycorporatedomain.com/.test(value); } , "Please specify the correct domain"); jQuery.validator.addMethod("nowhite", function(value) { return /^\S+$/.test(value); }, "No white space please"); jQuery.validator.addMethod("nowhite", function(value) { return /^\d+$/.test(value); }, "Please enter only digits");
 
Test-driven development
 
 
Tests first
Behaviour-driven developement
Why bother?
QUnit
test("my ajax code", function() { expect(1); stop(); $.get("myurl", function(response) { equals(response, "expected response"); start(); }); });
Implementing
(function($) {   $.fn.plugin = function() {   return this.each(function() {   // your code here   });   }; })(jQuery);
(function($) {   $.fn.delegate = function(type, delegate, handler) {   return this.bind(type, function(event) {   var target = $(event.target);   if (target.is(delegate)) {   return handler.apply(target, arguments);   }   });   };  })(jQuery);
(function($) {   $.fn.plugin = function(settings) {   settings = $.extend({}, $.plugin.defaults, settings);   return this.each(function() {   // your code here   });   };   $.plugin = {   defaults: {}   }; })(jQuery);
Document
/** * The number of elements currently matched. * * @example $(&quot;img&quot;).length; * @before <img src=&quot;test1.jpg&quot;/> <img src=&quot;test2.jpg&quot;/> * @result 2 * * @property * @name length * @type Number * @cat Core */ /** * The number of elements currently matched. * * @example $(&quot;img&quot;).size(); * @before <img src=&quot;test1.jpg&quot;/> <img src=&quot;test2.jpg&quot;/> * @result 2 * * @name size * @type Number * @cat Core */ size: function() { return this.length; }, length: 0,
Purpose
Dependencies
Pulic API
Options
Examples
Browsing
 
Release
jquery.plugin-1.2.3.zip - jquery-plugin-1.2.3 jquery.plugin.js jquery.plugin.min.js jquery.plugin.pack.js - demo [-docs] [-test]
<target name=&quot;tooltip&quot;>   <antcall target=&quot;generic&quot;>   <param name=&quot;name&quot; value=&quot;tooltip&quot; />   </antcall> </target> ant tooltip
1.3 --- * Added fade option (duration in ms) for fading in/out tooltips; IE <= 6 is excluded when bgiframe plugin is included * Fixed imagemaps in IE, added back example * Added positionLeft-option - positions the tooltip to the left of the cursor * Remove deprecated $.fn.Tooltip in favor of $.fn.tooltip 1.2 --- * Improved bodyHandler option to accept HTML strings, DOM elements and jQuery objects as the return value * Fixed bug in Safari 3 where to tooltip is initially visible, by first appending to DOM then hiding it * Improvement for viewport-border-positioning: Add the classes &quot;viewport-right&quot; and &quot;viewport-bottom&quot; when the element is moved at the viewport border. * Moved and enhanced documentation to docs.jquery.com * Added examples for bodyHandler: footnote-tooltip and thumbnail * Added id option, defaults to &quot;tooltip&quot;, override to use a different id in your stylesheet * Moved demo tooltip style to screen.css * Moved demo files to demo folder and dependencies to lib folder * Dropped image map example - completely incompatible with IE; image maps aren't supported anymore
 
 
Maintain
Avoid Blog Comments
 
 
 
 
 
Mailing list
 
 
General questions
Tracking
Thank you
Questions?
Bon appétit!
Ad

Recommended

AngularJS $Provide Service
AngularJS $Provide Service
Eyal Vardi
 
Intro to Angular.JS Directives
Intro to Angular.JS Directives
Christian Lilley
 
AngularJS Directives
AngularJS Directives
Eyal Vardi
 
AngularJS Routing
AngularJS Routing
Eyal Vardi
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the new
Remy Sharp
 
Cyclejs introduction
Cyclejs introduction
Arye Lukashevski
 
Mocking Demystified
Mocking Demystified
Marcello Duarte
 
Opinionated AngularJS
Opinionated AngularJS
prabhutech
 
AngularJS Internal
AngularJS Internal
Eyal Vardi
 
AngularJS Compile Process
AngularJS Compile Process
Eyal Vardi
 
AngularJS Services
AngularJS Services
Eyal Vardi
 
AngularJS Animations
AngularJS Animations
Eyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
Marcello Duarte
 
Angular js routing options
Angular js routing options
Nir Kaufman
 
AngularJS with TypeScript and Windows Azure Mobile Services
AngularJS with TypeScript and Windows Azure Mobile Services
Rainer Stropek
 
Angular Directives from Scratch
Angular Directives from Scratch
Christian Lilley
 
Intro to Ember.js
Intro to Ember.js
Jay Phelps
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4Developers
Kacper Gunia
 
Crafting beautiful software
Crafting beautiful software
Jorn Oomen
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 
Think Generic - Add API's To Your Custom Modules
Think Generic - Add API's To Your Custom Modules
Jens Sørensen
 
Система рендеринга в Magento
Система рендеринга в Magento
Magecom Ukraine
 
AngulrJS Overview
AngulrJS Overview
Eyal Vardi
 
jQuery in 15 minutes
jQuery in 15 minutes
Simon Willison
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
AngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
Angular Promises and Advanced Routing
Angular Promises and Advanced Routing
Alexe Bogdan
 
Clean Javascript
Clean Javascript
Ryunosuke SATO
 
Hooks WCSD12
Hooks WCSD12
Jeffrey Zinn
 

More Related Content

What's hot (20)

AngularJS Internal
AngularJS Internal
Eyal Vardi
 
AngularJS Compile Process
AngularJS Compile Process
Eyal Vardi
 
AngularJS Services
AngularJS Services
Eyal Vardi
 
AngularJS Animations
AngularJS Animations
Eyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
Marcello Duarte
 
Angular js routing options
Angular js routing options
Nir Kaufman
 
AngularJS with TypeScript and Windows Azure Mobile Services
AngularJS with TypeScript and Windows Azure Mobile Services
Rainer Stropek
 
Angular Directives from Scratch
Angular Directives from Scratch
Christian Lilley
 
Intro to Ember.js
Intro to Ember.js
Jay Phelps
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4Developers
Kacper Gunia
 
Crafting beautiful software
Crafting beautiful software
Jorn Oomen
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 
Think Generic - Add API's To Your Custom Modules
Think Generic - Add API's To Your Custom Modules
Jens Sørensen
 
Система рендеринга в Magento
Система рендеринга в Magento
Magecom Ukraine
 
AngulrJS Overview
AngulrJS Overview
Eyal Vardi
 
jQuery in 15 minutes
jQuery in 15 minutes
Simon Willison
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
AngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
Angular Promises and Advanced Routing
Angular Promises and Advanced Routing
Alexe Bogdan
 
AngularJS Internal
AngularJS Internal
Eyal Vardi
 
AngularJS Compile Process
AngularJS Compile Process
Eyal Vardi
 
AngularJS Services
AngularJS Services
Eyal Vardi
 
AngularJS Animations
AngularJS Animations
Eyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
Marcello Duarte
 
Angular js routing options
Angular js routing options
Nir Kaufman
 
AngularJS with TypeScript and Windows Azure Mobile Services
AngularJS with TypeScript and Windows Azure Mobile Services
Rainer Stropek
 
Angular Directives from Scratch
Angular Directives from Scratch
Christian Lilley
 
Intro to Ember.js
Intro to Ember.js
Jay Phelps
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4Developers
Kacper Gunia
 
Crafting beautiful software
Crafting beautiful software
Jorn Oomen
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 
Think Generic - Add API's To Your Custom Modules
Think Generic - Add API's To Your Custom Modules
Jens Sørensen
 
Система рендеринга в Magento
Система рендеринга в Magento
Magecom Ukraine
 
AngulrJS Overview
AngulrJS Overview
Eyal Vardi
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
AngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
Angular Promises and Advanced Routing
Angular Promises and Advanced Routing
Alexe Bogdan
 

Similar to Building Robust jQuery Plugins (20)

Clean Javascript
Clean Javascript
Ryunosuke SATO
 
Hooks WCSD12
Hooks WCSD12
Jeffrey Zinn
 
Building Large jQuery Applications
Building Large jQuery Applications
Rebecca Murphey
 
Secrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
jQuery secrets
jQuery secrets
Bastian Feder
 
Extend sdk
Extend sdk
Harsha Nagaraj
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
Fernando Daciuk
 
Building High Performance Web Applications and Sites
Building High Performance Web Applications and Sites
goodfriday
 
Introduction to JQuery
Introduction to JQuery
MobME Technical
 
Bubbles & Trees with jQuery
Bubbles & Trees with jQuery
Bastian Feder
 
Lithium Best
Lithium Best
Richard McIntyre
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5a
rajivmordani
 
Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2
Christoffer Noring
 
jQuery Internals + Cool Stuff
jQuery Internals + Cool Stuff
jeresig
 
Unittests für Dummies
Unittests für Dummies
Lars Jankowfsky
 
the next web now
the next web now
zulin Gu
 
Effecient javascript
Effecient javascript
mpnkhan
 
Virtual Madness @ Etsy
Virtual Madness @ Etsy
Nishan Subedi
 
The Beauty of Java Script
The Beauty of Java Script
Michael Girouard
 
Building Large jQuery Applications
Building Large jQuery Applications
Rebecca Murphey
 
Secrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
Fernando Daciuk
 
Building High Performance Web Applications and Sites
Building High Performance Web Applications and Sites
goodfriday
 
Bubbles & Trees with jQuery
Bubbles & Trees with jQuery
Bastian Feder
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5a
rajivmordani
 
jQuery Internals + Cool Stuff
jQuery Internals + Cool Stuff
jeresig
 
the next web now
the next web now
zulin Gu
 
Effecient javascript
Effecient javascript
mpnkhan
 
Virtual Madness @ Etsy
Virtual Madness @ Etsy
Nishan Subedi
 
Ad

Recently uploaded (20)

Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
Ad

Building Robust jQuery Plugins

  • 3. $.each({ focus: 'focusin', blur: 'focusout' }, function( original, fix ){ $.event.special[fix] = { setup:function() { if ( $.browser.msie ) return false; this.addEventListener( original, $.event.special[fix].handler, true ); }, teardown:function() { if ( $.browser.msie ) return false; this.removeEventListener( original, $.event.special[fix].handler, true ); }, handler: function(e) { arguments[0] = $.event.fix(e); arguments[0].type = fix; return $.event.handle.apply(this, arguments); } }; });
  • 5. “ Given enough eyeballs, all bugs are shallow ” -- Eric S. Raymond
  • 9.  
  • 10.  
  • 11.  
  • 12. jQuery.validator. addMethod (&quot;domain&quot;, function(value) { return /^http://mycorporatedomain.com/.test(value); } , &quot;Please specify the correct domain&quot;); jQuery.validator.addMethod(&quot;nowhite&quot;, function(value) { return /^\S+$/.test(value); }, &quot;No white space please&quot;); jQuery.validator.addMethod(&quot;nowhite&quot;, function(value) { return /^\d+$/.test(value); }, &quot;Please enter only digits&quot;);
  • 13.  
  • 15.  
  • 16.  
  • 20. QUnit
  • 21. test(&quot;my ajax code&quot;, function() { expect(1); stop(); $.get(&quot;myurl&quot;, function(response) { equals(response, &quot;expected response&quot;); start(); }); });
  • 23. (function($) { $.fn.plugin = function() { return this.each(function() { // your code here }); }; })(jQuery);
  • 24. (function($) { $.fn.delegate = function(type, delegate, handler) { return this.bind(type, function(event) { var target = $(event.target); if (target.is(delegate)) { return handler.apply(target, arguments); } }); }; })(jQuery);
  • 25. (function($) { $.fn.plugin = function(settings) { settings = $.extend({}, $.plugin.defaults, settings); return this.each(function() { // your code here }); }; $.plugin = { defaults: {} }; })(jQuery);
  • 27. /** * The number of elements currently matched. * * @example $(&quot;img&quot;).length; * @before <img src=&quot;test1.jpg&quot;/> <img src=&quot;test2.jpg&quot;/> * @result 2 * * @property * @name length * @type Number * @cat Core */ /** * The number of elements currently matched. * * @example $(&quot;img&quot;).size(); * @before <img src=&quot;test1.jpg&quot;/> <img src=&quot;test2.jpg&quot;/> * @result 2 * * @name size * @type Number * @cat Core */ size: function() { return this.length; }, length: 0,
  • 34.  
  • 36. jquery.plugin-1.2.3.zip - jquery-plugin-1.2.3 jquery.plugin.js jquery.plugin.min.js jquery.plugin.pack.js - demo [-docs] [-test]
  • 37. <target name=&quot;tooltip&quot;> <antcall target=&quot;generic&quot;> <param name=&quot;name&quot; value=&quot;tooltip&quot; /> </antcall> </target> ant tooltip
  • 38. 1.3 --- * Added fade option (duration in ms) for fading in/out tooltips; IE <= 6 is excluded when bgiframe plugin is included * Fixed imagemaps in IE, added back example * Added positionLeft-option - positions the tooltip to the left of the cursor * Remove deprecated $.fn.Tooltip in favor of $.fn.tooltip 1.2 --- * Improved bodyHandler option to accept HTML strings, DOM elements and jQuery objects as the return value * Fixed bug in Safari 3 where to tooltip is initially visible, by first appending to DOM then hiding it * Improvement for viewport-border-positioning: Add the classes &quot;viewport-right&quot; and &quot;viewport-bottom&quot; when the element is moved at the viewport border. * Moved and enhanced documentation to docs.jquery.com * Added examples for bodyHandler: footnote-tooltip and thumbnail * Added id option, defaults to &quot;tooltip&quot;, override to use a different id in your stylesheet * Moved demo tooltip style to screen.css * Moved demo files to demo folder and dependencies to lib folder * Dropped image map example - completely incompatible with IE; image maps aren't supported anymore
  • 39.  
  • 40.  
  • 43.  
  • 44.  
  • 45.  
  • 46.  
  • 47.  
  • 49.  
  • 50.  

Editor's Notes

  • #2: Goal is to show to how build a robust jQuery plugin, way beyond just coding. Your take-away should be an understanding of the surrounding infrastructure, both to be able to use and to help improve it. We&apos;ve got about 50 minutes. My talk should take about 30 minutes, giving us 20 minutes for open discussion. Please ask questions whenever you want to, though I may defer some answers to the end. To start, lets look at the most important question.