SlideShare une entreprise Scribd logo
1  sur  95
Télécharger pour lire hors ligne
Hell is other browsers - Sartre




JavaScript Events
     Peter-Paul Koch (ppk)
    http://quirksmode.org
    http://twitter.com/ppk
     Yahoo!, 23 April 2009
http://quirksmode.org/dom/events/
Today's program:
- the key events
- the change event
- delegating the focus event
- first results of mobile event tests
The key events
keydown
  When a key is depressed.
  Repeats.
keypress


keyup
keydown
  When a key is depressed.
  Repeats.
keypress
  When a character key is
  depressed.
  Repeats.
keyup
keydown
  When a key is depressed.
  Repeats.
keypress
  When a character key is
  depressed.
  Repeats.
keyup
  When a key is released.
keydown and keypress
keydown only
Originally this theory was created
by Microsoft.

Safari has copied it.

It's the only theory; Firefox and
Opera just fire some random
events.
keydown
  When a key is depressed.
  Repeats.
keypress
  When a character key is
  depressed.
  Repeats.
Which key did my user press?

Two properties:
keyCode and charCode

Two bits of data:
- the key code
- the character code
Which key did my user press?

Obviously, having one property
contain one bit of data and the other
property the other

would be far too simple.
Which key did my user press?

keyCode

- onkeydown: key code
- onkeypress: character code
Which key did my user press?

charCode

- onkeydown: 0
- onkeypress: character code
Which key did my user press?

If you need the key:

el.onkeydown = function (e) {
  e = e || window.event;
  var realKey = e.keyCode;
}
Which key did my user press?

If you need the key:

el.onkeydown = function (e) {
  e = e || window.event;
  var realKey = e.keyCode;
}
Which key did my user press?

If you need the character:

el.onkeypress = function (e) {
  e = e || window.event;
  var char = e.keyCode || e.charCode;
}
Which key did my user press?

If you need the character:

el.onkeypress = function (e) {
  e = e || window.event;
  var char = e.keyCode || e.charCode;
}
How can I prevent the default action?

el.onkeydown = function (e) {
  e = e || window.event;
  var key = e.keyCode;
  if (key is incorrect) {
     // cancel default action
  }
}
How can I prevent the default action?

el.onkeydown = function (e) {
  e = e || window.event;
  var key = e.keyCode;
  if (key is incorrect) {
     // cancel default action
  }
}
change
The change event fires when the value
of a form field is changed.

This could be a very useful event; after
all it fires only when the user actually
changes something
instead of whenever he focuses on a
form field
- text fields
- select boxes
- checkboxes and radios
- text fields
- select boxes
- checkboxes and radios

              focus

              blur

No change event. The value hasn't
been modified.
- text fields
- select boxes
- checkboxes and radios

              focus

              blur

Change event. The value has been
modified.
- text fields
- select boxes
- checkboxes and radios

Mouse:



Click on select
- text fields
- select boxes
- checkboxes and radios

Mouse:



Click on new option
CHANGE
- text fields
- select boxes
- checkboxes and radios

Keyboard:

   focus


Focus on select
- text fields
- select boxes
- checkboxes and radios

Keyboard:

   focus    arrow


Arrow keys to move to other option
CHANGE
- text fields
- select boxes
- checkboxes and radios

        This is a
        BUG!
Arrow keys to move to other option
CHANGE
- text fields
- select boxes
- checkboxes and radios

Keyboard:

   focus    arrow


Arrow keys to move to other option
- text fields
- select boxes
- checkboxes and radios

Keyboard:

   focus     arrow        blur


Blur select box.
CHANGE
- text fields
- select boxes
- checkboxes and radios



  click

CHANGE when the checked property
changes.
- text fields
- select boxes
- checkboxes and radios



      click

...
- text fields
- select boxes
- checkboxes and radios



  click    blur

CHANGE when the element loses the
focus.
- text fields
- select boxes
- checkboxes and radios

         This is a
         BUG!
CHANGE when the element loses the
focus.
Event delegation
Event delegation
<ul>
  <li><a href=”#”>Multimedialize</a>
     <ul>
       <li><a href=”#”>Sound</a></li>
       <li><a href=”#”>Java applets</a></li>
     </ul></li>
  <li><a href=”#”>Ajaxify</a>
     <ul>
       <li><a href=”#”>Web 2.0</a></li>
       <li><a href=”#”>Web 3.0</a></li>
       <li><a href=”#”>Web 4.0b</a></li>
     </ul></li>
</ul>
Event delegation
Event delegation
  The event bubbles up to the <ul>
var dropdown = {
  anyway. (dropdown) {
   init: function
      dropdown.onmouseover = mouseOver;
      dropdown.onmouseout = mouseOut;
  So why not handle it at that level?
   }
}
Saves a lot of event handlers.
Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = mouseOver;
     dropdown.onmouseout = mouseOut;
  }
}

Works in all browsers.
 Event delegation

But suppose someone doesn't use a
mouse at all,

but the keyboard

how does the menu fold out?
Device
independence
 Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  }
}
Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  }
}

Doesn't work without a mouse.
Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  }
}

We need events that tell us whether
the user enters or leaves a link.
focus and blur
Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover =
        dropdown.onfocus = this.mouseOver;
     dropdown.onmouseout =
        dropdown.onblur = this.mouseOut;
  }
}
Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover =
        dropdown.onfocus = this.mouseOver;
     dropdown.onmouseout =
        dropdown.onblur = this.mouseOut;
  }
}

Doesn't work.
Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover =
        dropdown.onfocus = this.mouseOver;
     dropdown.onmouseout =
        dropdown.onblur = this.mouseOut;
  }
}

Focus and blur don't bubble.
To bubble or not to bubble

Two kinds of events:
1) Mouse and key events
2) Interface events
To bubble or not to bubble

Two kinds of events:
1) Mouse and key events
2) Interface events

Fire when the user initiates a device-
specific action.
mouseover, mouseout, click, keydown,
keypress
To bubble or not to bubble

Two kinds of events:
1) Mouse and key events
2) Interface events

In general they bubble.
To bubble or not to bubble

Two kinds of events:
1) Mouse and key events
2) Interface events

Fire when a certain event takes place,
regardless of how it was initialised.
load, change, submit, focus, blur
To bubble or not to bubble

Two kinds of events:
1) Mouse and key events
2) Interface events

In general they don't bubble.
Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
     var x = dropdown.getElementsByTagName('a');
     for (var i=0;i<x.length;i++) {
        x[i].onfocus = this.mouseOver;
        x[i].onblur = this.mouseOut;
     }
  }
}
Event delegation

So we're stuck with setting a focus and
blur event on every single link.

Or are we ... ?
Event delegation

Event capturing to the rescue.

Event capturing is the opposite of
event bubbling,
and it is supported in all W3C-
compliant browsers.
Event bubbling
addEventListener('click',fn,false)
Event capturing
addEventListener('click',fn,true)
Event delegation

The point is that if you capture a focus
event,

event handlers on the target element's
ancestors are executed.
Event bubbling
Focus on a:
a.onfocus executed
Event capturing
Focus on a: ul.onfocus, li.onfocus and
a.onfocus executed
 Event capturing and bubbling

Why this difference?

I have no idea.

Maybe it's time to have bubbling and
capturing act the same.
The current situation doesn't make
sense (though it's useful).
 Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  }
}
 Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
     dropdown.addEventListener
        ('focus',this.mouseOver,true);
     dropdown.addEventListener
        ('blur',this.mouseOut,true);
  }
}
 Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
     if (dropdown.addEventListener) {
         dropdown.addEventListener
           ('focus',this.mouseOver,true);
         dropdown.addEventListener
           ('blur',this.mouseOut,true);
     }
  }
}
 Event delegation
And what about IE?

It doesn't support addEventListener,
but fortunately it supports the
focusin and focusout events
which are like focus and blur,
except that they bubble.
 Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
     if (dropdown.addEventListener) {
         dropdown.addEventListener
           ('focus',this.mouseOver,true);
         dropdown.addEventListener
           ('blur',this.mouseOut,true);
     }
  }
}
 Event delegation

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
     if (dropdown.addEventListener) {
         dropdown.addEventListener
           ('focus',this.mouseOver,true);
         dropdown.addEventListener
           ('blur',this.mouseOut,true);
     }
     dropdown.onfocusin = this.mouseOver;
     dropdown.onfocusout = this.mouseOut
  }
 Event delegation

   dropdown.onmouseover = this.mouseOver;
   dropdown.onmouseout = this.mouseOut;
   if (dropdown.addEventListener) {
       dropdown.addEventListener
         ('focus',this.mouseOver,true);
       dropdown.addEventListener
         ('blur',this.mouseOut,true);
   }
   dropdown.onfocusin = this.mouseOver;
   dropdown.onfocusout = this.mouseOut;
Device
independence
Mobile phones

The Mobile Web is finally coming,
and thanks to Vodafone I'm able to
give you some information,
although the subject remains a tricky
one.
Mobile phones – input modes

On mobile phones there are three
input modes:
- Touch
- Cursor (or rather, pseudo-cursor)
- Four-way navigation (“arrow” keys)
Opera Mini 4.2
on Nokia E71

Pseudo-
cursor input
mode
Opera Mobile
8.00 on
Motorola V3xx

Four-way
navigation
NetFront on
Sony Ericsson
K770i

Four-way
navigation,
but which
link do you
follow when
you click?
Mobile phones – events

In such an environment, what does
“mouseover” mean?

And what about mouseout,
mousemove, mousedown, mouseup?

And click?
Mobile phones – events

I set up a test in which I “click” on a
<div> element and see which events
take place.

First some good news:
S60 WebKit on
Nokia E71
Input: cursor

The same as
desktop
browsers
Opera Mobile
9.5 on HTC
Touch Diamond
Input: touch

This is the
same as the
iPhone does.
Mobile phones – events

So Nokia cursor phones behave
exactly as desktop computers,

while the latest Opera behaves exactly
as the iPhone.
Mobile phones – events

iPhone/Opera model:

As soon as the user touches an element
mouseover, mousemove, mousedown,
mouseup, and click fire,
and the :hover styles are applied.

When user touches another element, mouseout
fires and :hover styles are removed
Mobile phones – events

Now for some bad news.
Blackberry
Storm
Input: touch

No
mouseover,
-out, or
-move
NetFront on
Samsung F700
Input: touch

Where's the
click?
Mobile phones – events

These are only 4 of the 19 browsers I
tested,
and there are hundreds of browsers on
mobile phones.
Mobile phones – events

General rules for events on mobile
phones 1/3:
- use click (and let's politely but firmly give
the finger to browsers that do not support it)
- forget about the mouse events

In fact, I think the time has come to
retire the mouse events on all devices
(with a few exceptions)
Mobile phones – events

General rules for events on mobile
phones 2/3:
- use the resize AND the
orientationchange event

orientationchange is supported only by iPhone
and Blackberry
resize is supported by Opera and all WebKits
NetFront doesn't support either – too bad
Mobile phones – events

General rules for events on mobile
phones 3/3:
- use key events only for setting
general access keys;
and NOT for reading out user input in a form
field

You should read out the field's value instead.
Event compatibility for desktop:

http://quirksmode.org/dom/events

Mobile compatibility
(work in progress)

http://quirksmode.org/m/
Thank you
for your attention
Questions?
Ask away.
Or ask me on Twitter
http://twitter.com/ppk
or on my site
http://quirksmode.org

Contenu connexe

Tendances

JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
Robert Nyman
 
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
Robert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
Robert Nyman
 
Special Events: Beyond Custom Events
Special Events: Beyond Custom EventsSpecial Events: Beyond Custom Events
Special Events: Beyond Custom Events
Brandon Aaron
 
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresJavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
Robert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
Robert Nyman
 
Leave No One Behind with HTML5 - FFWD.PRO, Croatia
Leave No One Behind with HTML5 - FFWD.PRO, CroatiaLeave No One Behind with HTML5 - FFWD.PRO, Croatia
Leave No One Behind with HTML5 - FFWD.PRO, Croatia
Robert Nyman
 
HTML5, The Open Web, and what it means for you - MDN Hack Day, Sao Paulo
HTML5, The Open Web, and what it means for you - MDN Hack Day, Sao PauloHTML5, The Open Web, and what it means for you - MDN Hack Day, Sao Paulo
HTML5, The Open Web, and what it means for you - MDN Hack Day, Sao Paulo
Robert Nyman
 

Tendances (20)

JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
 
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
 
Special Events: Beyond Custom Events
Special Events: Beyond Custom EventsSpecial Events: Beyond Custom Events
Special Events: Beyond Custom Events
 
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresJavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
 
Client Web
Client WebClient Web
Client Web
 
jQuery: Events, Animation, Ajax
jQuery: Events, Animation, AjaxjQuery: Events, Animation, Ajax
jQuery: Events, Animation, Ajax
 
Getting Touchy Feely with the Web
Getting Touchy Feely with the WebGetting Touchy Feely with the Web
Getting Touchy Feely with the Web
 
KODE JS POKENNNNN
KODE JS POKENNNNNKODE JS POKENNNNN
KODE JS POKENNNNN
 
The Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
The Wonderful-Amazing-Orientation-Motion-Sensormatic MachineThe Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
The Wonderful-Amazing-Orientation-Motion-Sensormatic Machine
 
Meet VueJs
Meet VueJsMeet VueJs
Meet VueJs
 
How kris-writes-symfony-apps-london
How kris-writes-symfony-apps-londonHow kris-writes-symfony-apps-london
How kris-writes-symfony-apps-london
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
 
Leave No One Behind with HTML5 - FFWD.PRO, Croatia
Leave No One Behind with HTML5 - FFWD.PRO, CroatiaLeave No One Behind with HTML5 - FFWD.PRO, Croatia
Leave No One Behind with HTML5 - FFWD.PRO, Croatia
 
amsterdamjs - jQuery 1.5
amsterdamjs - jQuery 1.5amsterdamjs - jQuery 1.5
amsterdamjs - jQuery 1.5
 
Events Lecture Notes
Events Lecture NotesEvents Lecture Notes
Events Lecture Notes
 
JavaScript From Scratch: Events
JavaScript From Scratch: EventsJavaScript From Scratch: Events
JavaScript From Scratch: Events
 
HTML5, The Open Web, and what it means for you - MDN Hack Day, Sao Paulo
HTML5, The Open Web, and what it means for you - MDN Hack Day, Sao PauloHTML5, The Open Web, and what it means for you - MDN Hack Day, Sao Paulo
HTML5, The Open Web, and what it means for you - MDN Hack Day, Sao Paulo
 
Getting classy with ES6
Getting classy with ES6Getting classy with ES6
Getting classy with ES6
 

En vedette (7)

Understanding JavaScript Event-based Interactions
Understanding JavaScript Event-based InteractionsUnderstanding JavaScript Event-based Interactions
Understanding JavaScript Event-based Interactions
 
Dive into javascript event
Dive into javascript eventDive into javascript event
Dive into javascript event
 
JavaScript event handling assignment
JavaScript  event handling assignment JavaScript  event handling assignment
JavaScript event handling assignment
 
JavaScript and BOM events
JavaScript and BOM eventsJavaScript and BOM events
JavaScript and BOM events
 
Javascript and DOM
Javascript and DOMJavascript and DOM
Javascript and DOM
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 

Similaire à Yahoo presentation: JavaScript Events

Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Patrick Lauke
 
There is something about Event
There is something about EventThere is something about Event
There is something about Event
Eddie Kao
 
Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Patrick Lauke
 
jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
dmethvin
 
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Patrick Lauke
 
Flash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic listFlash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic list
Small Screen Design
 
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Patrick Lauke
 
Composite Applications with WPF and PRISM
Composite Applications with WPF and PRISMComposite Applications with WPF and PRISM
Composite Applications with WPF and PRISM
Eyal Vardi
 

Similaire à Yahoo presentation: JavaScript Events (20)

Voices That Matter: JavaScript Events
Voices That Matter: JavaScript EventsVoices That Matter: JavaScript Events
Voices That Matter: JavaScript Events
 
Getting touchy - an introduction to touch and pointer events / TPAC 2016 / Li...
Getting touchy - an introduction to touch and pointer events / TPAC 2016 / Li...Getting touchy - an introduction to touch and pointer events / TPAC 2016 / Li...
Getting touchy - an introduction to touch and pointer events / TPAC 2016 / Li...
 
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
 
There is something about Event
There is something about EventThere is something about Event
There is something about Event
 
The touch events
The touch eventsThe touch events
The touch events
 
Touchevents
ToucheventsTouchevents
Touchevents
 
Signalsで Event処理を簡単に
Signalsで Event処理を簡単にSignalsで Event処理を簡単に
Signalsで Event処理を簡単に
 
2011 07-hiyoko
2011 07-hiyoko2011 07-hiyoko
2011 07-hiyoko
 
Events.pdf
Events.pdfEvents.pdf
Events.pdf
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
 
Mobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptMobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScript
 
jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
 
Java Event Handling
Java Event HandlingJava Event Handling
Java Event Handling
 
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Flash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic listFlash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic list
 
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
 
Event handling
Event handlingEvent handling
Event handling
 
Composite Applications with WPF and PRISM
Composite Applications with WPF and PRISMComposite Applications with WPF and PRISM
Composite Applications with WPF and PRISM
 

Plus de Peter-Paul Koch

Rethinking the mobile web
Rethinking the mobile webRethinking the mobile web
Rethinking the mobile web
Peter-Paul Koch
 
The future of the mobile web
The future of the mobile webThe future of the mobile web
The future of the mobile web
Peter-Paul Koch
 
The mobile browser world
The mobile browser worldThe mobile browser world
The mobile browser world
Peter-Paul Koch
 
The touch events - WebExpo
The touch events - WebExpoThe touch events - WebExpo
The touch events - WebExpo
Peter-Paul Koch
 

Plus de Peter-Paul Koch (14)

Rethinking the mobile web
Rethinking the mobile webRethinking the mobile web
Rethinking the mobile web
 
The mobile browser world
The mobile browser worldThe mobile browser world
The mobile browser world
 
The future of the mobile web
The future of the mobile webThe future of the mobile web
The future of the mobile web
 
The mobile browser world
The mobile browser worldThe mobile browser world
The mobile browser world
 
The touch events
The touch eventsThe touch events
The touch events
 
JSON over SMS
JSON over SMSJSON over SMS
JSON over SMS
 
The touch events - WebExpo
The touch events - WebExpoThe touch events - WebExpo
The touch events - WebExpo
 
Samsung
SamsungSamsung
Samsung
 
The Mobile Web - Fronteers 2009
The Mobile Web - Fronteers 2009The Mobile Web - Fronteers 2009
The Mobile Web - Fronteers 2009
 
State of the Mobile Browsers
State of the Mobile BrowsersState of the Mobile Browsers
State of the Mobile Browsers
 
Vodafone Widget Camp
Vodafone Widget CampVodafone Widget Camp
Vodafone Widget Camp
 
The Ajax Experience: State Of The Browsers
The Ajax Experience: State Of The BrowsersThe Ajax Experience: State Of The Browsers
The Ajax Experience: State Of The Browsers
 
An Event Apart Boston: Principles of Unobtrusive JavaScript
An Event Apart Boston: Principles of Unobtrusive JavaScriptAn Event Apart Boston: Principles of Unobtrusive JavaScript
An Event Apart Boston: Principles of Unobtrusive JavaScript
 
Google presentation: The Open Web goes mobile
Google presentation: The Open Web goes mobileGoogle presentation: The Open Web goes mobile
Google presentation: The Open Web goes mobile
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

Yahoo presentation: JavaScript Events

  • 1. Hell is other browsers - Sartre JavaScript Events Peter-Paul Koch (ppk) http://quirksmode.org http://twitter.com/ppk Yahoo!, 23 April 2009
  • 2.
  • 4. Today's program: - the key events - the change event - delegating the focus event - first results of mobile event tests
  • 6. keydown When a key is depressed. Repeats. keypress keyup
  • 7. keydown When a key is depressed. Repeats. keypress When a character key is depressed. Repeats. keyup
  • 8. keydown When a key is depressed. Repeats. keypress When a character key is depressed. Repeats. keyup When a key is released.
  • 11. Originally this theory was created by Microsoft. Safari has copied it. It's the only theory; Firefox and Opera just fire some random events.
  • 12. keydown When a key is depressed. Repeats. keypress When a character key is depressed. Repeats.
  • 13. Which key did my user press? Two properties: keyCode and charCode Two bits of data: - the key code - the character code
  • 14. Which key did my user press? Obviously, having one property contain one bit of data and the other property the other would be far too simple.
  • 15. Which key did my user press? keyCode - onkeydown: key code - onkeypress: character code
  • 16. Which key did my user press? charCode - onkeydown: 0 - onkeypress: character code
  • 17. Which key did my user press? If you need the key: el.onkeydown = function (e) { e = e || window.event; var realKey = e.keyCode; }
  • 18. Which key did my user press? If you need the key: el.onkeydown = function (e) { e = e || window.event; var realKey = e.keyCode; }
  • 19. Which key did my user press? If you need the character: el.onkeypress = function (e) { e = e || window.event; var char = e.keyCode || e.charCode; }
  • 20. Which key did my user press? If you need the character: el.onkeypress = function (e) { e = e || window.event; var char = e.keyCode || e.charCode; }
  • 21. How can I prevent the default action? el.onkeydown = function (e) { e = e || window.event; var key = e.keyCode; if (key is incorrect) { // cancel default action } }
  • 22. How can I prevent the default action? el.onkeydown = function (e) { e = e || window.event; var key = e.keyCode; if (key is incorrect) { // cancel default action } }
  • 24. The change event fires when the value of a form field is changed. This could be a very useful event; after all it fires only when the user actually changes something instead of whenever he focuses on a form field
  • 25. - text fields - select boxes - checkboxes and radios
  • 26. - text fields - select boxes - checkboxes and radios focus blur No change event. The value hasn't been modified.
  • 27. - text fields - select boxes - checkboxes and radios focus blur Change event. The value has been modified.
  • 28. - text fields - select boxes - checkboxes and radios Mouse: Click on select
  • 29. - text fields - select boxes - checkboxes and radios Mouse: Click on new option CHANGE
  • 30. - text fields - select boxes - checkboxes and radios Keyboard: focus Focus on select
  • 31. - text fields - select boxes - checkboxes and radios Keyboard: focus arrow Arrow keys to move to other option CHANGE
  • 32. - text fields - select boxes - checkboxes and radios This is a BUG! Arrow keys to move to other option CHANGE
  • 33. - text fields - select boxes - checkboxes and radios Keyboard: focus arrow Arrow keys to move to other option
  • 34. - text fields - select boxes - checkboxes and radios Keyboard: focus arrow blur Blur select box. CHANGE
  • 35. - text fields - select boxes - checkboxes and radios click CHANGE when the checked property changes.
  • 36. - text fields - select boxes - checkboxes and radios click ...
  • 37. - text fields - select boxes - checkboxes and radios click blur CHANGE when the element loses the focus.
  • 38. - text fields - select boxes - checkboxes and radios This is a BUG! CHANGE when the element loses the focus.
  • 40. Event delegation <ul> <li><a href=”#”>Multimedialize</a> <ul> <li><a href=”#”>Sound</a></li> <li><a href=”#”>Java applets</a></li> </ul></li> <li><a href=”#”>Ajaxify</a> <ul> <li><a href=”#”>Web 2.0</a></li> <li><a href=”#”>Web 3.0</a></li> <li><a href=”#”>Web 4.0b</a></li> </ul></li> </ul>
  • 42. Event delegation The event bubbles up to the <ul> var dropdown = { anyway. (dropdown) { init: function dropdown.onmouseover = mouseOver; dropdown.onmouseout = mouseOut; So why not handle it at that level? } } Saves a lot of event handlers.
  • 43. Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = mouseOver; dropdown.onmouseout = mouseOut; } } Works in all browsers.
  • 44.  Event delegation But suppose someone doesn't use a mouse at all, but the keyboard how does the menu fold out?
  • 46.  Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; } }
  • 47. Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; } } Doesn't work without a mouse.
  • 48. Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; } } We need events that tell us whether the user enters or leaves a link. focus and blur
  • 49. Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = dropdown.onfocus = this.mouseOver; dropdown.onmouseout = dropdown.onblur = this.mouseOut; } }
  • 50. Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = dropdown.onfocus = this.mouseOver; dropdown.onmouseout = dropdown.onblur = this.mouseOut; } } Doesn't work.
  • 51. Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = dropdown.onfocus = this.mouseOver; dropdown.onmouseout = dropdown.onblur = this.mouseOut; } } Focus and blur don't bubble.
  • 52. To bubble or not to bubble Two kinds of events: 1) Mouse and key events 2) Interface events
  • 53. To bubble or not to bubble Two kinds of events: 1) Mouse and key events 2) Interface events Fire when the user initiates a device- specific action. mouseover, mouseout, click, keydown, keypress
  • 54. To bubble or not to bubble Two kinds of events: 1) Mouse and key events 2) Interface events In general they bubble.
  • 55. To bubble or not to bubble Two kinds of events: 1) Mouse and key events 2) Interface events Fire when a certain event takes place, regardless of how it was initialised. load, change, submit, focus, blur
  • 56. To bubble or not to bubble Two kinds of events: 1) Mouse and key events 2) Interface events In general they don't bubble.
  • 57. Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; var x = dropdown.getElementsByTagName('a'); for (var i=0;i<x.length;i++) { x[i].onfocus = this.mouseOver; x[i].onblur = this.mouseOut; } } }
  • 58. Event delegation So we're stuck with setting a focus and blur event on every single link. Or are we ... ?
  • 59. Event delegation Event capturing to the rescue. Event capturing is the opposite of event bubbling, and it is supported in all W3C- compliant browsers.
  • 62. Event delegation The point is that if you capture a focus event, event handlers on the target element's ancestors are executed.
  • 63. Event bubbling Focus on a: a.onfocus executed
  • 64. Event capturing Focus on a: ul.onfocus, li.onfocus and a.onfocus executed
  • 65.  Event capturing and bubbling Why this difference? I have no idea. Maybe it's time to have bubbling and capturing act the same. The current situation doesn't make sense (though it's useful).
  • 66.  Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; } }
  • 67.  Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; dropdown.addEventListener ('focus',this.mouseOver,true); dropdown.addEventListener ('blur',this.mouseOut,true); } }
  • 68.  Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; if (dropdown.addEventListener) { dropdown.addEventListener ('focus',this.mouseOver,true); dropdown.addEventListener ('blur',this.mouseOut,true); } } }
  • 69.  Event delegation And what about IE? It doesn't support addEventListener, but fortunately it supports the focusin and focusout events which are like focus and blur, except that they bubble.
  • 70.  Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; if (dropdown.addEventListener) { dropdown.addEventListener ('focus',this.mouseOver,true); dropdown.addEventListener ('blur',this.mouseOut,true); } } }
  • 71.  Event delegation var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; if (dropdown.addEventListener) { dropdown.addEventListener ('focus',this.mouseOver,true); dropdown.addEventListener ('blur',this.mouseOut,true); } dropdown.onfocusin = this.mouseOver; dropdown.onfocusout = this.mouseOut }
  • 72.  Event delegation dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; if (dropdown.addEventListener) { dropdown.addEventListener ('focus',this.mouseOver,true); dropdown.addEventListener ('blur',this.mouseOut,true); } dropdown.onfocusin = this.mouseOver; dropdown.onfocusout = this.mouseOut;
  • 74.
  • 75. Mobile phones The Mobile Web is finally coming, and thanks to Vodafone I'm able to give you some information, although the subject remains a tricky one.
  • 76. Mobile phones – input modes On mobile phones there are three input modes: - Touch - Cursor (or rather, pseudo-cursor) - Four-way navigation (“arrow” keys)
  • 77. Opera Mini 4.2 on Nokia E71 Pseudo- cursor input mode
  • 78. Opera Mobile 8.00 on Motorola V3xx Four-way navigation
  • 79. NetFront on Sony Ericsson K770i Four-way navigation, but which link do you follow when you click?
  • 80. Mobile phones – events In such an environment, what does “mouseover” mean? And what about mouseout, mousemove, mousedown, mouseup? And click?
  • 81. Mobile phones – events I set up a test in which I “click” on a <div> element and see which events take place. First some good news:
  • 82. S60 WebKit on Nokia E71 Input: cursor The same as desktop browsers
  • 83. Opera Mobile 9.5 on HTC Touch Diamond Input: touch This is the same as the iPhone does.
  • 84. Mobile phones – events So Nokia cursor phones behave exactly as desktop computers, while the latest Opera behaves exactly as the iPhone.
  • 85. Mobile phones – events iPhone/Opera model: As soon as the user touches an element mouseover, mousemove, mousedown, mouseup, and click fire, and the :hover styles are applied. When user touches another element, mouseout fires and :hover styles are removed
  • 86. Mobile phones – events Now for some bad news.
  • 88. NetFront on Samsung F700 Input: touch Where's the click?
  • 89. Mobile phones – events These are only 4 of the 19 browsers I tested, and there are hundreds of browsers on mobile phones.
  • 90. Mobile phones – events General rules for events on mobile phones 1/3: - use click (and let's politely but firmly give the finger to browsers that do not support it) - forget about the mouse events In fact, I think the time has come to retire the mouse events on all devices (with a few exceptions)
  • 91. Mobile phones – events General rules for events on mobile phones 2/3: - use the resize AND the orientationchange event orientationchange is supported only by iPhone and Blackberry resize is supported by Opera and all WebKits NetFront doesn't support either – too bad
  • 92. Mobile phones – events General rules for events on mobile phones 3/3: - use key events only for setting general access keys; and NOT for reading out user input in a form field You should read out the field's value instead.
  • 93. Event compatibility for desktop: http://quirksmode.org/dom/events Mobile compatibility (work in progress) http://quirksmode.org/m/
  • 94. Thank you for your attention
  • 95. Questions? Ask away. Or ask me on Twitter http://twitter.com/ppk or on my site http://quirksmode.org