SlideShare a Scribd company logo
JQUERY 1.7 EVENTS
1.7 Event Goals: Simplify the API
1.7 Event Goals: Make It Faster!
1.7 Event Goals: Reduce Overall Size
1.7 Event Goals: Fix Existing Bugs
1.7 Event Goals: Don’t Break It
jQuery 1.7 Events
.on()  and  .off() Unifies bound and delegated events $().on(events, “selector”, data, handler); $().off(events, “selector”, handler); Maps straightforwardly to old APIs: $(“a”).bind(“click change”, { mydata: 42 }, fn); $(“a”).on(“click change”, { mydata: 42 }, fn); $(“.box”).delegate(“a”, “click.myplugin”, fn); $(“.box”).on(“click.myplugin”, “a”, fn); $(“a”).live(“click”, fn); $(document).on(“click”, “a”, fn);
Benefits Ends confusion about interaction across event APIs $( “a” ). live ( “click” , function(){ alert( “Hello” ); }); $(document). unbind ( “click” );  // live is now dead! $(document). on ( “click” ,  “a” , function(){ alert( “Hello” ); }); $(document). off ( “click” ); Consistent pronunciation (unlike  live/die ) Chainable with traversal/filter methods (unlike  live ) Argument order is consistent (unlike  delegate ) Shorter to type ( off  vs.  undelegate ) No confusion with ES5  Function.bind Saved 1,122 bytes removing bind/live/delegate in 1.7
jQuery 1.7 Events
Yes: bind, live, delegate work in 1.7!
No need to panic
Event Delegation is Great, But … W3C sez: Determine handlers  before  delivery A handler can’t change which subsequent handlers are called, other than  .stopPropagation()  and  .stopImmediatePropagation() jQuery  must  selector-match up to the delegation point Protip: D elegate to the nearest possible element
Don’t Make Me Bubble, Bro! (document)  $(“.submenu-item”).live(“click”, myFn ) <html> <body class=“js”> <div id=“wrapper”> <ul id=“nav”>  $(“#nav”).on(“click”, “.submenu-item”, myFn ) <li class=“flyout”> Text size <ul class=“submenu”> <li class=“submenu-item”>Medium</li> Browser bubbles to the delegation point, calls the jQuery handler jQuery selector-matches,  again  bubbling to the delegation point jQuery runs the user’s handler(s) (in this case, myFn)
No Pressure, But …
Defeat Selector-Match Bottlenecks!
Test Case Profile in 1.6.3:  15.1 secs
Test Case Profile in 1.7:  5.6 secs
Therefore …
JavaScript is Slower than C … …  but we can be smarter than  matchesSelector! Reprise John Resig’s “The Selectors People Actually Use” From Google Codesearch, for  .live()   and  .delegate() 33%  .class 12%  tag.class 11%  #id 4%  tag 4%  tag[attr=value] 68% match  tag#id.class[attr=value]
A Plan Materializes:  quickIs() Attempt to pre-parse the selector during event attach If it matches the subset, save the parsed components At event delivery, selector check is just one statement Complex selectors work, they just take the Sizzle path (Show the codez)
jQuery 1.7 Events
How Fast Is It Now? Delegated event delivery time (milliseconds): Let us not speak of event overhead again. Browser jQuery 1.6.3 jQuery 1.7 Beta Chrome 13 3.0 1.1 (2.8x) Firefox 6 4.0 1.5 (2.7x) IE 7 55.6 13.1 (4.2x) IE 9 9.6 2.3 (4.1x) IE10 PP 8.2 2.1 (3.9x)
Event Hook Enhancements
Make Events More Hookable Existing code had some hard-wired special cases: mouseenter/mouseleave  on delegated events Delegated events put under a “live” event – not DRY New special event hooks simplified code: trigger  hook called from  jQuery.event.trigger() handle  hook called from  jQuery.event.handle() New event property hooks (propHooks) Fixes up  event  object when a  native  event occurs Allows code to run only for specific events Copies only relevant event properties Example: Normalize  event.pageX/pageY  for mouse events
Internet Explorer Can’t Be Forgotten
Change/Submit for IE 6, 7, 8 A must-have for useful event delegation jQuery special event hooks for old IE, since 1.4 Fake bubbling submit event by listening for click/keypress Fake bubbling change event by listening for lots o’ stuff Unfortunately, fake isn’t always good enough Events don’t fire in expected order Complex special cases that require lots of code to fix Heroic failed effort, so let’s try Plan B: Listen for the earliest possible event on an element Attach a real submit/change event and bubble it manually jQuery cleanup takes care of removing the event
Event Bugs Fixed in 1.7 Many fixed in the course of overall code restructure Oldest bug was from February 2010 Open Bugs Open Feature Requests jQuery 1.6.4 28 4 jQuery 1.7 beta 3 1
In Summary …
It Passes Unit Tests, But …
We Need Your Help Testing
It Do All Things! What You Think? Google+, email: dave.methvin@gmail.com Twitter: @davemethvin Github: dmethvin
And now … OM NOM NOM!

More Related Content

PPTX
Getting the Most Out of jQuery Widgets
PPTX
FuncUnit
PDF
jQuery: Nuts, Bolts and Bling
KEY
jQuery('#knowledge').appendTo('#you');
PDF
jQuery in the [Aol.] Enterprise
PDF
Write Less Do More
PPT
JQuery introduction
PDF
jQuery in 15 minutes
Getting the Most Out of jQuery Widgets
FuncUnit
jQuery: Nuts, Bolts and Bling
jQuery('#knowledge').appendTo('#you');
jQuery in the [Aol.] Enterprise
Write Less Do More
JQuery introduction
jQuery in 15 minutes

What's hot (20)

PDF
Matters of State
PDF
Organizing Code with JavascriptMVC
PDF
DOM Scripting Toolkit - jQuery
KEY
jQuery Performance Tips and Tricks (2011)
PPTX
jQuery Presentasion
PDF
Functionality Focused Code Organization
PDF
Javascript in Plone
PDF
jQuery: Events, Animation, Ajax
PDF
How Kris Writes Symfony Apps
PDF
How Kris Writes Symfony Apps
PDF
How kris-writes-symfony-apps-london
PPTX
Jquery Complete Presentation along with Javascript Basics
KEY
Sprout core and performance
PDF
History of jQuery
PDF
Rails 3: Dashing to the Finish
PDF
Drupal, meet Assetic
PDF
Sane Async Patterns
PDF
Learning jQuery in 30 minutes
PDF
Dojo Confessions
PDF
Angular Directives from Scratch
Matters of State
Organizing Code with JavascriptMVC
DOM Scripting Toolkit - jQuery
jQuery Performance Tips and Tricks (2011)
jQuery Presentasion
Functionality Focused Code Organization
Javascript in Plone
jQuery: Events, Animation, Ajax
How Kris Writes Symfony Apps
How Kris Writes Symfony Apps
How kris-writes-symfony-apps-london
Jquery Complete Presentation along with Javascript Basics
Sprout core and performance
History of jQuery
Rails 3: Dashing to the Finish
Drupal, meet Assetic
Sane Async Patterns
Learning jQuery in 30 minutes
Dojo Confessions
Angular Directives from Scratch
Ad

Viewers also liked (8)

ODP
Schenker - DSL for quickly creating web applications in Perl
PDF
Write good parser in perl
PPTX
Customized story map for agrg weather network
ZIP
Presentational jQuery
PDF
How to make your corporate event go viral using social media
KEY
Natural Language UI Testing using Behavior Driven Development with Pavlov and...
PPTX
Sano hmm 20150512
PPTX
Sano tokyowebmining 201625_v04
Schenker - DSL for quickly creating web applications in Perl
Write good parser in perl
Customized story map for agrg weather network
Presentational jQuery
How to make your corporate event go viral using social media
Natural Language UI Testing using Behavior Driven Development with Pavlov and...
Sano hmm 20150512
Sano tokyowebmining 201625_v04
Ad

Similar to jQuery 1.7 Events (20)

ODP
Event handling using jQuery
KEY
jQuery Events
PDF
jQuery Behaviours
PDF
PPTX
jQuery Best Practice
PPT
Digesting jQuery
PDF
J query fundamentals
PPTX
Jquery introduce
KEY
jQuery - Tips And Tricks
PPTX
Javascript And J Query
PPT
JavaScript Libraries
PDF
Jquery In Action Second Edition 2nd Ed Bear Bibeault Yehuda Katz
PPTX
Getting Started with jQuery
PPTX
Introduction to Jquery
PDF
State of jQuery and Drupal
PPT
J query module1
PPT
JavaScript JQUERY AJAX
PDF
Introduction to jQuery
PDF
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
PDF
Introducing jQuery
Event handling using jQuery
jQuery Events
jQuery Behaviours
jQuery Best Practice
Digesting jQuery
J query fundamentals
Jquery introduce
jQuery - Tips And Tricks
Javascript And J Query
JavaScript Libraries
Jquery In Action Second Edition 2nd Ed Bear Bibeault Yehuda Katz
Getting Started with jQuery
Introduction to Jquery
State of jQuery and Drupal
J query module1
JavaScript JQUERY AJAX
Introduction to jQuery
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Introducing jQuery

More from dmethvin (12)

PPTX
HTTP 2.0 - Web Unleashed 2015
PPTX
jQuery Foot-Gun Features
PPTX
jQuery Conference Chicago - September 2014
PDF
PrairieDevCon 2014 - Web Doesn't Mean Slow
PDF
jQuery Conference Toronto
PDF
jQuery Conference San Diego 2014 - Web Performance
PPT
State of jQuery - AspDotNetStorefront Conference
PPTX
jQuery Conference Austin Sept 2013
PDF
State of jQuery June 2013 - Portland
PDF
jQueryTO: State of jQuery March 2013
PDF
jQuery Features to Avoid
PPTX
jQuery Conference 2012 keynote
HTTP 2.0 - Web Unleashed 2015
jQuery Foot-Gun Features
jQuery Conference Chicago - September 2014
PrairieDevCon 2014 - Web Doesn't Mean Slow
jQuery Conference Toronto
jQuery Conference San Diego 2014 - Web Performance
State of jQuery - AspDotNetStorefront Conference
jQuery Conference Austin Sept 2013
State of jQuery June 2013 - Portland
jQueryTO: State of jQuery March 2013
jQuery Features to Avoid
jQuery Conference 2012 keynote

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
Big Data Technologies - Introduction.pptx
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Advanced IT Governance
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced Soft Computing BINUS July 2025.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Big Data Technologies - Introduction.pptx
GamePlan Trading System Review: Professional Trader's Honest Take
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
The AUB Centre for AI in Media Proposal.docx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Advanced IT Governance
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing

jQuery 1.7 Events

  • 2. 1.7 Event Goals: Simplify the API
  • 3. 1.7 Event Goals: Make It Faster!
  • 4. 1.7 Event Goals: Reduce Overall Size
  • 5. 1.7 Event Goals: Fix Existing Bugs
  • 6. 1.7 Event Goals: Don’t Break It
  • 8. .on() and .off() Unifies bound and delegated events $().on(events, “selector”, data, handler); $().off(events, “selector”, handler); Maps straightforwardly to old APIs: $(“a”).bind(“click change”, { mydata: 42 }, fn); $(“a”).on(“click change”, { mydata: 42 }, fn); $(“.box”).delegate(“a”, “click.myplugin”, fn); $(“.box”).on(“click.myplugin”, “a”, fn); $(“a”).live(“click”, fn); $(document).on(“click”, “a”, fn);
  • 9. Benefits Ends confusion about interaction across event APIs $( “a” ). live ( “click” , function(){ alert( “Hello” ); }); $(document). unbind ( “click” ); // live is now dead! $(document). on ( “click” , “a” , function(){ alert( “Hello” ); }); $(document). off ( “click” ); Consistent pronunciation (unlike live/die ) Chainable with traversal/filter methods (unlike live ) Argument order is consistent (unlike delegate ) Shorter to type ( off vs. undelegate ) No confusion with ES5 Function.bind Saved 1,122 bytes removing bind/live/delegate in 1.7
  • 11. Yes: bind, live, delegate work in 1.7!
  • 12. No need to panic
  • 13. Event Delegation is Great, But … W3C sez: Determine handlers before delivery A handler can’t change which subsequent handlers are called, other than .stopPropagation() and .stopImmediatePropagation() jQuery must selector-match up to the delegation point Protip: D elegate to the nearest possible element
  • 14. Don’t Make Me Bubble, Bro! (document) $(“.submenu-item”).live(“click”, myFn ) <html> <body class=“js”> <div id=“wrapper”> <ul id=“nav”> $(“#nav”).on(“click”, “.submenu-item”, myFn ) <li class=“flyout”> Text size <ul class=“submenu”> <li class=“submenu-item”>Medium</li> Browser bubbles to the delegation point, calls the jQuery handler jQuery selector-matches, again bubbling to the delegation point jQuery runs the user’s handler(s) (in this case, myFn)
  • 17. Test Case Profile in 1.6.3: 15.1 secs
  • 18. Test Case Profile in 1.7: 5.6 secs
  • 20. JavaScript is Slower than C … … but we can be smarter than matchesSelector! Reprise John Resig’s “The Selectors People Actually Use” From Google Codesearch, for .live() and .delegate() 33% .class 12% tag.class 11% #id 4% tag 4% tag[attr=value] 68% match tag#id.class[attr=value]
  • 21. A Plan Materializes: quickIs() Attempt to pre-parse the selector during event attach If it matches the subset, save the parsed components At event delivery, selector check is just one statement Complex selectors work, they just take the Sizzle path (Show the codez)
  • 23. How Fast Is It Now? Delegated event delivery time (milliseconds): Let us not speak of event overhead again. Browser jQuery 1.6.3 jQuery 1.7 Beta Chrome 13 3.0 1.1 (2.8x) Firefox 6 4.0 1.5 (2.7x) IE 7 55.6 13.1 (4.2x) IE 9 9.6 2.3 (4.1x) IE10 PP 8.2 2.1 (3.9x)
  • 25. Make Events More Hookable Existing code had some hard-wired special cases: mouseenter/mouseleave on delegated events Delegated events put under a “live” event – not DRY New special event hooks simplified code: trigger hook called from jQuery.event.trigger() handle hook called from jQuery.event.handle() New event property hooks (propHooks) Fixes up event object when a native event occurs Allows code to run only for specific events Copies only relevant event properties Example: Normalize event.pageX/pageY for mouse events
  • 27. Change/Submit for IE 6, 7, 8 A must-have for useful event delegation jQuery special event hooks for old IE, since 1.4 Fake bubbling submit event by listening for click/keypress Fake bubbling change event by listening for lots o’ stuff Unfortunately, fake isn’t always good enough Events don’t fire in expected order Complex special cases that require lots of code to fix Heroic failed effort, so let’s try Plan B: Listen for the earliest possible event on an element Attach a real submit/change event and bubble it manually jQuery cleanup takes care of removing the event
  • 28. Event Bugs Fixed in 1.7 Many fixed in the course of overall code restructure Oldest bug was from February 2010 Open Bugs Open Feature Requests jQuery 1.6.4 28 4 jQuery 1.7 beta 3 1
  • 30. It Passes Unit Tests, But …
  • 31. We Need Your Help Testing
  • 32. It Do All Things! What You Think? Google+, email: dave.methvin@gmail.com Twitter: @davemethvin Github: dmethvin
  • 33. And now … OM NOM NOM!