SlideShare una empresa de Scribd logo
1 de 95
Descargar para leer sin conexión
Hell is other browsers - Sartre




 JavaScript Events
           Peter-Paul Koch (ppk)
           http://quirksmode.org
          http://twitter.com/ppk
Voices that Matter, 28 April 2009
http://quirksmode.org/dom/events/
Mouseover


 and friends
The mouseover event fires when the
user's mouse enters an element .

The mouseout event fires when the
user's mouse leaves an element.

Perfect support
Dropdown menu <sigh />
<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>
Dropdown menu <sigh />
Dropdown menu <sigh />
Dropdown menu <sigh />

Event bubbling has advantages.
var dropdown = {
  init: function (dropdown) {
     var x = dropdown.getElementsByTagName('a');
     for (var i=0;i<x.length;i++) {
        x[i].onmouseover = mouseOver;
        x[i].onmouseout = mouseOut;
     }
  }
}
Dropdown menu <sigh />

Event bubbling has advantages.
var dropdown = {
  init: function (dropdown) {
     var x = dropdown.getElementsByTagName('a');
     for (var i=0;i<x.length;i++) {
        x[i].onmouseover = mouseOver;
        x[i].onmouseout = mouseOut;
     }
  }
}
Dropdown menu <sigh />

Event bubbling has advantages.
var dropdown = {
  init: function (dropdown) {

We don't do this any more. Instead
we use event delegation.
    }
}
Dropdown menu <sigh />
  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.
Dropdown menu <sigh />

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

Works in all browsers.
Dropdown menu <sigh />

Problem: Every mouseover or
mouseout event bubbles up.
Dropdown menu <sigh />
Dropdown menu <sigh />

a.mouseover
a.mouseout and a.mouseover
a.mouseout and a.mouseover
a.mouseout

Fun!
Event bubbling works.
As does event delegation.
Dropdown menu <sigh />

a.mouseover
a.mouseout and a.mouseover
a.mouseout and a.mouseover
a.mouseout

But has the mouse left the submenu or
not?!
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  },
  mouseOut: function (e) {
     if (this mouseout is important) {
         this.closeSubMenu();
     }
  }
}
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  },
  mouseOut: function (e) {
     if (this mouseout is important) {
         this.closeSubMenu();
     }
  }
}
Development time: about 10 minutes
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  },
  mouseOut: function (e) {
     if (this mouseout is important) {
         this.closeSubMenu();
     }
  }
}
Development time: about 2 days
Dropdown menu <sigh />

How do we do this?

onmouseout, find out which element
the mouse goes to.

If that element is not a part of the
submenu, fold the submenu.
Dropdown menu <sigh />

How do we do this?


 mouseOut: function (e) {
   e = e || window.event;
   var el = e.relatedTarget || e.toElement;
   if (!submenu.contains(el)) {
       this.closeSubMenu();
   }
 }
Dropdown menu <sigh />

Find the element the mouse goes to.


 mouseOut: function (e) {
   e = e || window.event;
   var el = e.relatedTarget || e.toElement;
   if (!submenu.contains(el)) {
       this.closeSubMenu();
   }
 }
Dropdown menu <sigh />

Find the element the mouse goes to.


 mouseOut: function (e) {
   e = e || window.event;
   var el = e.relatedTarget || e.toElement;
   if (!submenu.contains(el)) {
       this.closeSubMenu();
   }
 }
Dropdown menu <sigh />

Find the element the mouse goes to.


 mouseOut: function (e) {
   e = e || window.event;
   var el = e.relatedTarget || e.toElement;
   if (!submenu.contains(el)) {
       this.closeSubMenu();
   }
 }
Dropdown menu <sigh />

See whether that element is contained
by the submenu.

 mouseOut: function (e) {
   e = e || window.event;
   var el = e.relatedTarget || e.toElement;
   if (!submenu.contains(el)) {
       this.closeSubMenu();
   }
 }
Dropdown menu <sigh />

See whether that element is contained
by the submenu.

 mouseOut: function (e) {
   e = e || window.event;
   var el = e.relatedTarget || e.toElement;
   if (!submenu.contains(el)) {
       this.closeSubMenu();
   }
 }
Dropdown menu <sigh />
That's it, right?

<grin type=”evil” />
 mouseOut: function (e) {
   e = e || window.event;
   var el = e.relatedTarget || e.toElement;
   if (!submenu.contains(el)) {
       this.closeSubMenu();
   }
 }
Dropdown menu <sigh />

Wrong!

Suppose someone doesn't use a mouse
at all,

but the keyboard

how does the menu fold out?
Device
independence
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  }
}
Dropdown menu <sigh />

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

Doesn't work without a mouse.
Dropdown menu <sigh />

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
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover =
        dropdown.onfocus = this.mouseOver;
     dropdown.onmouseout =
        dropdown.onblur = this.mouseOut;
  }
}
Dropdown menu <sigh />

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

Doesn't work.
Dropdown menu <sigh />

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

Generally don't bubble
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
  }
}
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
     var x = dropdown.getElementsByTagName('li');
     for (var i=0;i<x.length;i++) {
        x[i].onfocus = this.mouseOver;
        x[i].onblur = this.mouseOut;
     }
  }
}
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
     var x = dropdown.getElementsByTagName('li');
     for (var i=0;i<x.length;i++) {
        x[i].onfocus = this.mouseOver;
        x[i].onblur = this.mouseOut;
     }
  }
}
Doesn't work.
Dropdown menu <sigh />

The HTML elements must be able to
receive the keyboard focus.

- links
- form fields
Dropdown menu <sigh />

The HTML elements must be able to
receive the keyboard focus.

- links
- form fields
- elements with tabindex
Dropdown menu <sigh />

var dropdown = {
  init: function (dropdown) {
     dropdown.onmouseover = this.mouseOver;
     dropdown.onmouseout = this.mouseOut;
     var x = dropdown.getElementsByTagName('li');
     for (var i=0;i<x.length;i++) {
        x[i].onfocus = this.mouseOver;
        x[i].onblur = this.mouseOut;
     }
  }
}
Dropdown menu <sigh />

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;
     }
  }
}
Dropdown menu <sigh />

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 ... ?

In my recent Yahoo! presentation I
give an outline of the solution.
http://yuiblog.com/blog/2009/04/27/video-ppk-jsevents/
More device
independence
And what about click?

We're in luck: click also fires when the
user activates an element by keyboard.

Restriction:
the element must be able to receive
the keyboard focus
Separate concepts

Drag-and-drop
uses the mousemove event
Separate concepts

Drag-and-drop
uses the mousemove event

and if there's one thing that's
impossible to emulate with the
keyboard

it's moving the mouse
Separate concepts

Drag-and-drop
uses the mousemove event

How do we make this keyboard
accessible?

By allowing the user to use the arrow
keys.
Key events.
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?

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

el.onkeydown = function (e) {
  e = e || window.event;
  var realKey = e.keyCode;
}
Separate concepts

Back to the drag-and-drop
Separate concepts

Drag-and-drop

We need the keydown event, because
arrow keys are special keys.
Separate concepts

Drag-and-drop

obj.onmousemove =
 obj.onkeydown = moveElement;
Separate concepts

Drag-and-drop

obj.onmousemove =
 obj.onkeydown = moveElement;

Doesn't work.
Separate concepts

Drag-and-drop

obj.onmousemove =
 obj.onkeydown = moveElement;

Mousemove expects mouse
coordinates.
The layer moves to these coordinates.
Separate concepts

Drag-and-drop

obj.onmousemove =
 obj.onkeydown = moveElement;

The key events expect a keystroke.
obj.onkeydown = function (e) {
  e = e || window.event;
  var key = e.keyCode;
  switch (key) {
       case 37: // left
       case 38: // up
       case 39: // right
       case 40: // down
          return false;
       default:
          return true;
     }
}
Separate concepts

But what does “user hits right arrow
once” mean?

10px?
50px?
“Move to next receptor element?”
Something else that fits your interface?
Separate concepts

Drag-and-drop

We have to program for two totally
different situations.
We need separate scripts.

obj.onmousemove = moveByMouse;
obj.onkeydown = moveByKeys;
Separate concepts

Drag-and-drop

Yes, that's more work.

But if you do it right you've got a
generic drag and drop module you can
use anywhere.
Separate concepts

Drag-and-drop

Besides, I created a first draft for you.

http://quirksmode.org/js/dragdrop.html
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.
http://quirksmode.org/dom/events/
Questions?
Ask away.
Or ask me on Twitter
http://twitter.com/ppk
or on my site
http://quirksmode.org

Más contenido relacionado

La actualidad más candente

Patrick H. Lauke - Getting Touchy; an introduction to touch and pointer events
Patrick H. Lauke - Getting Touchy; an introduction to touch and pointer eventsPatrick H. Lauke - Getting Touchy; an introduction to touch and pointer events
Patrick H. Lauke - Getting Touchy; an introduction to touch and pointer eventsDevConFu
 
дыдыкин егор
дыдыкин егордыдыкин егор
дыдыкин егорkuchinskaya
 
Getting touchy - an introduction to touch and pointer events / Sainté Mobile ...
Getting touchy - an introduction to touch and pointer events / Sainté Mobile ...Getting touchy - an introduction to touch and pointer events / Sainté Mobile ...
Getting touchy - an introduction to touch and pointer events / Sainté Mobile ...Patrick Lauke
 
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.2013Patrick Lauke
 
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...Patrick Lauke
 
The touch events - WebExpo
The touch events - WebExpoThe touch events - WebExpo
The touch events - WebExpoPeter-Paul Koch
 
Getting touchy - an introduction to touch and pointer events (1 day workshop)...
Getting touchy - an introduction to touch and pointer events (1 day workshop)...Getting touchy - an introduction to touch and pointer events (1 day workshop)...
Getting touchy - an introduction to touch and pointer events (1 day workshop)...Patrick Lauke
 
Getting touchy - an introduction to touch and pointer events / Smashing Confe...
Getting touchy - an introduction to touch and pointer events / Smashing Confe...Getting touchy - an introduction to touch and pointer events / Smashing Confe...
Getting touchy - an introduction to touch and pointer events / Smashing Confe...Patrick Lauke
 
Getting touchy - an introduction to touch and pointer events (1 day workshop)...
Getting touchy - an introduction to touch and pointer events (1 day workshop)...Getting touchy - an introduction to touch and pointer events (1 day workshop)...
Getting touchy - an introduction to touch and pointer events (1 day workshop)...Patrick Lauke
 
Wii Ruby All Work And No Play Just Wont Do
Wii Ruby All Work And No Play Just Wont DoWii Ruby All Work And No Play Just Wont Do
Wii Ruby All Work And No Play Just Wont DoLittleBIGRuby
 
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...Patrick Lauke
 
Getting touchy - an introduction to touch and pointer events / Workshop / Jav...
Getting touchy - an introduction to touch and pointer events / Workshop / Jav...Getting touchy - an introduction to touch and pointer events / Workshop / Jav...
Getting touchy - an introduction to touch and pointer events / Workshop / Jav...Patrick Lauke
 
10 awt event model
10 awt event model10 awt event model
10 awt event modelBayarkhuu
 
Developing social simulations with UbikSim
Developing social simulations with UbikSimDeveloping social simulations with UbikSim
Developing social simulations with UbikSimEmilio Serrano
 
The Fast and The Mobile - Matteo Antony Mistretta & Giada Cazzola - Codemotio...
The Fast and The Mobile - Matteo Antony Mistretta & Giada Cazzola - Codemotio...The Fast and The Mobile - Matteo Antony Mistretta & Giada Cazzola - Codemotio...
The Fast and The Mobile - Matteo Antony Mistretta & Giada Cazzola - Codemotio...Codemotion
 
Getting touchy - an introduction to touch and pointer events (Workshop) / Jav...
Getting touchy - an introduction to touch and pointer events (Workshop) / Jav...Getting touchy - an introduction to touch and pointer events (Workshop) / Jav...
Getting touchy - an introduction to touch and pointer events (Workshop) / Jav...Patrick Lauke
 

La actualidad más candente (17)

Patrick H. Lauke - Getting Touchy; an introduction to touch and pointer events
Patrick H. Lauke - Getting Touchy; an introduction to touch and pointer eventsPatrick H. Lauke - Getting Touchy; an introduction to touch and pointer events
Patrick H. Lauke - Getting Touchy; an introduction to touch and pointer events
 
дыдыкин егор
дыдыкин егордыдыкин егор
дыдыкин егор
 
Getting touchy - an introduction to touch and pointer events / Sainté Mobile ...
Getting touchy - an introduction to touch and pointer events / Sainté Mobile ...Getting touchy - an introduction to touch and pointer events / Sainté Mobile ...
Getting touchy - an introduction to touch and pointer events / Sainté Mobile ...
 
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
 
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
 
The touch events - WebExpo
The touch events - WebExpoThe touch events - WebExpo
The touch events - WebExpo
 
Getting touchy - an introduction to touch and pointer events (1 day workshop)...
Getting touchy - an introduction to touch and pointer events (1 day workshop)...Getting touchy - an introduction to touch and pointer events (1 day workshop)...
Getting touchy - an introduction to touch and pointer events (1 day workshop)...
 
Getting touchy - an introduction to touch and pointer events / Smashing Confe...
Getting touchy - an introduction to touch and pointer events / Smashing Confe...Getting touchy - an introduction to touch and pointer events / Smashing Confe...
Getting touchy - an introduction to touch and pointer events / Smashing Confe...
 
Getting touchy - an introduction to touch and pointer events (1 day workshop)...
Getting touchy - an introduction to touch and pointer events (1 day workshop)...Getting touchy - an introduction to touch and pointer events (1 day workshop)...
Getting touchy - an introduction to touch and pointer events (1 day workshop)...
 
Wii Ruby All Work And No Play Just Wont Do
Wii Ruby All Work And No Play Just Wont DoWii Ruby All Work And No Play Just Wont Do
Wii Ruby All Work And No Play Just Wont Do
 
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
 
Getting touchy - an introduction to touch and pointer events / Workshop / Jav...
Getting touchy - an introduction to touch and pointer events / Workshop / Jav...Getting touchy - an introduction to touch and pointer events / Workshop / Jav...
Getting touchy - an introduction to touch and pointer events / Workshop / Jav...
 
10 awt event model
10 awt event model10 awt event model
10 awt event model
 
Developing social simulations with UbikSim
Developing social simulations with UbikSimDeveloping social simulations with UbikSim
Developing social simulations with UbikSim
 
The touch events
The touch eventsThe touch events
The touch events
 
The Fast and The Mobile - Matteo Antony Mistretta & Giada Cazzola - Codemotio...
The Fast and The Mobile - Matteo Antony Mistretta & Giada Cazzola - Codemotio...The Fast and The Mobile - Matteo Antony Mistretta & Giada Cazzola - Codemotio...
The Fast and The Mobile - Matteo Antony Mistretta & Giada Cazzola - Codemotio...
 
Getting touchy - an introduction to touch and pointer events (Workshop) / Jav...
Getting touchy - an introduction to touch and pointer events (Workshop) / Jav...Getting touchy - an introduction to touch and pointer events (Workshop) / Jav...
Getting touchy - an introduction to touch and pointer events (Workshop) / Jav...
 

Similar a Voices That Matter: JavaScript Events

Yahoo presentation: JavaScript Events
Yahoo presentation: JavaScript EventsYahoo presentation: JavaScript Events
Yahoo presentation: JavaScript EventsPeter-Paul Koch
 
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
 
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 listSmall Screen Design
 
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.2013Patrick Lauke
 
There is something about Event
There is something about EventThere is something about Event
There is something about EventEddie Kao
 
Composite Applications with WPF and PRISM
Composite Applications with WPF and PRISMComposite Applications with WPF and PRISM
Composite Applications with WPF and PRISMEyal Vardi
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Javascript Experiment
Javascript ExperimentJavascript Experiment
Javascript Experimentwgamboa
 
Event handling63
Event handling63Event handling63
Event handling63myrajendra
 
Chapt 04 user interaction
Chapt 04 user interactionChapt 04 user interaction
Chapt 04 user interactionEdi Faizal
 
AR community meeting, Seoul, Korea, October 6, 2015
AR community meeting, Seoul, Korea, October 6, 2015AR community meeting, Seoul, Korea, October 6, 2015
AR community meeting, Seoul, Korea, October 6, 2015fridolin.wild
 
Keyboard Access APIs
Keyboard Access APIsKeyboard Access APIs
Keyboard Access APIstoddkloots
 
Event handling
Event handlingEvent handling
Event handlingswapnac12
 
Signalsで Event処理を簡単に
Signalsで Event処理を簡単にSignalsで Event処理を簡単に
Signalsで Event処理を簡単にHiroaki Okubo
 

Similar a Voices That Matter: JavaScript Events (20)

Yahoo presentation: JavaScript Events
Yahoo presentation: JavaScript EventsYahoo presentation: JavaScript Events
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...
 
Events
EventsEvents
Events
 
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 / 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
 
There is something about Event
There is something about EventThere is something about Event
There is something about Event
 
ev
evev
ev
 
The touch events
The touch eventsThe touch events
The touch events
 
Composite Applications with WPF and PRISM
Composite Applications with WPF and PRISMComposite Applications with WPF and PRISM
Composite Applications with WPF and PRISM
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Javascript Experiment
Javascript ExperimentJavascript Experiment
Javascript Experiment
 
Event handling63
Event handling63Event handling63
Event handling63
 
Java awt
Java awtJava awt
Java awt
 
Chapt 04 user interaction
Chapt 04 user interactionChapt 04 user interaction
Chapt 04 user interaction
 
AR community meeting, Seoul, Korea, October 6, 2015
AR community meeting, Seoul, Korea, October 6, 2015AR community meeting, Seoul, Korea, October 6, 2015
AR community meeting, Seoul, Korea, October 6, 2015
 
Keyboard Access APIs
Keyboard Access APIsKeyboard Access APIs
Keyboard Access APIs
 
Java
JavaJava
Java
 
Aloha Presentation #t3con10
Aloha Presentation #t3con10Aloha Presentation #t3con10
Aloha Presentation #t3con10
 
Event handling
Event handlingEvent handling
Event handling
 
Signalsで Event処理を簡単に
Signalsで Event処理を簡単にSignalsで Event処理を簡単に
Signalsで Event処理を簡単に
 

Más de Peter-Paul Koch

Rethinking the mobile web
Rethinking the mobile webRethinking the mobile web
Rethinking the mobile webPeter-Paul Koch
 
The mobile browser world
The mobile browser worldThe mobile browser world
The mobile browser worldPeter-Paul Koch
 
The future of the mobile web
The future of the mobile webThe future of the mobile web
The future of the mobile webPeter-Paul Koch
 
The mobile browser world
The mobile browser worldThe mobile browser world
The mobile browser worldPeter-Paul Koch
 
The Mobile Web - Fronteers 2009
The Mobile Web - Fronteers 2009The Mobile Web - Fronteers 2009
The Mobile Web - Fronteers 2009Peter-Paul Koch
 
State of the Mobile Browsers
State of the Mobile BrowsersState of the Mobile Browsers
State of the Mobile BrowsersPeter-Paul Koch
 
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 BrowsersPeter-Paul Koch
 
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 JavaScriptPeter-Paul Koch
 
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 mobilePeter-Paul Koch
 

Más de Peter-Paul Koch (12)

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
 
JSON over SMS
JSON over SMSJSON over SMS
JSON over SMS
 
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
 

Último

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
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, ...apidays
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
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 SavingEdi Saputra
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
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 businesspanagenda
 
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...apidays
 
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 WorkerThousandEyes
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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 FresherRemote DBA Services
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Último (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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, ...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
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
 
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...
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Voices That Matter: JavaScript Events

  • 1. Hell is other browsers - Sartre JavaScript Events Peter-Paul Koch (ppk) http://quirksmode.org http://twitter.com/ppk Voices that Matter, 28 April 2009
  • 2.
  • 5. The mouseover event fires when the user's mouse enters an element . The mouseout event fires when the user's mouse leaves an element. Perfect support
  • 6. Dropdown menu <sigh /> <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>
  • 9. Dropdown menu <sigh /> Event bubbling has advantages. var dropdown = { init: function (dropdown) { var x = dropdown.getElementsByTagName('a'); for (var i=0;i<x.length;i++) { x[i].onmouseover = mouseOver; x[i].onmouseout = mouseOut; } } }
  • 10. Dropdown menu <sigh /> Event bubbling has advantages. var dropdown = { init: function (dropdown) { var x = dropdown.getElementsByTagName('a'); for (var i=0;i<x.length;i++) { x[i].onmouseover = mouseOver; x[i].onmouseout = mouseOut; } } }
  • 11. Dropdown menu <sigh /> Event bubbling has advantages. var dropdown = { init: function (dropdown) { We don't do this any more. Instead we use event delegation. } }
  • 12. Dropdown menu <sigh /> 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.
  • 13. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = mouseOver; dropdown.onmouseout = mouseOut; } } Works in all browsers.
  • 14. Dropdown menu <sigh /> Problem: Every mouseover or mouseout event bubbles up.
  • 16. Dropdown menu <sigh /> a.mouseover a.mouseout and a.mouseover a.mouseout and a.mouseover a.mouseout Fun! Event bubbling works. As does event delegation.
  • 17. Dropdown menu <sigh /> a.mouseover a.mouseout and a.mouseover a.mouseout and a.mouseover a.mouseout But has the mouse left the submenu or not?!
  • 18. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; }, mouseOut: function (e) { if (this mouseout is important) { this.closeSubMenu(); } } }
  • 19. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; }, mouseOut: function (e) { if (this mouseout is important) { this.closeSubMenu(); } } } Development time: about 10 minutes
  • 20. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; }, mouseOut: function (e) { if (this mouseout is important) { this.closeSubMenu(); } } } Development time: about 2 days
  • 21. Dropdown menu <sigh /> How do we do this? onmouseout, find out which element the mouse goes to. If that element is not a part of the submenu, fold the submenu.
  • 22. Dropdown menu <sigh /> How do we do this? mouseOut: function (e) { e = e || window.event; var el = e.relatedTarget || e.toElement; if (!submenu.contains(el)) { this.closeSubMenu(); } }
  • 23. Dropdown menu <sigh /> Find the element the mouse goes to. mouseOut: function (e) { e = e || window.event; var el = e.relatedTarget || e.toElement; if (!submenu.contains(el)) { this.closeSubMenu(); } }
  • 24. Dropdown menu <sigh /> Find the element the mouse goes to. mouseOut: function (e) { e = e || window.event; var el = e.relatedTarget || e.toElement; if (!submenu.contains(el)) { this.closeSubMenu(); } }
  • 25. Dropdown menu <sigh /> Find the element the mouse goes to. mouseOut: function (e) { e = e || window.event; var el = e.relatedTarget || e.toElement; if (!submenu.contains(el)) { this.closeSubMenu(); } }
  • 26. Dropdown menu <sigh /> See whether that element is contained by the submenu. mouseOut: function (e) { e = e || window.event; var el = e.relatedTarget || e.toElement; if (!submenu.contains(el)) { this.closeSubMenu(); } }
  • 27. Dropdown menu <sigh /> See whether that element is contained by the submenu. mouseOut: function (e) { e = e || window.event; var el = e.relatedTarget || e.toElement; if (!submenu.contains(el)) { this.closeSubMenu(); } }
  • 28. Dropdown menu <sigh /> That's it, right? <grin type=”evil” /> mouseOut: function (e) { e = e || window.event; var el = e.relatedTarget || e.toElement; if (!submenu.contains(el)) { this.closeSubMenu(); } }
  • 29. Dropdown menu <sigh /> Wrong! Suppose someone doesn't use a mouse at all, but the keyboard how does the menu fold out?
  • 31. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; } }
  • 32. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; } } Doesn't work without a mouse.
  • 33. Dropdown menu <sigh /> 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
  • 34. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = dropdown.onfocus = this.mouseOver; dropdown.onmouseout = dropdown.onblur = this.mouseOut; } }
  • 35. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = dropdown.onfocus = this.mouseOver; dropdown.onmouseout = dropdown.onblur = this.mouseOut; } } Doesn't work.
  • 36. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = dropdown.onfocus = this.mouseOver; dropdown.onmouseout = dropdown.onblur = this.mouseOut; } } Focus and blur don't bubble.
  • 37. To bubble or not to bubble Two kinds of events: 1) Mouse and key events 2) Interface events
  • 38. 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
  • 39. To bubble or not to bubble Two kinds of events: 1) Mouse and key events 2) Interface events In general they bubble
  • 40. 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
  • 41. To bubble or not to bubble Two kinds of events: 1) Mouse and key events 2) Interface events Generally don't bubble
  • 42. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; } }
  • 43. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; var x = dropdown.getElementsByTagName('li'); for (var i=0;i<x.length;i++) { x[i].onfocus = this.mouseOver; x[i].onblur = this.mouseOut; } } }
  • 44. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; var x = dropdown.getElementsByTagName('li'); for (var i=0;i<x.length;i++) { x[i].onfocus = this.mouseOver; x[i].onblur = this.mouseOut; } } } Doesn't work.
  • 45. Dropdown menu <sigh /> The HTML elements must be able to receive the keyboard focus. - links - form fields
  • 46. Dropdown menu <sigh /> The HTML elements must be able to receive the keyboard focus. - links - form fields - elements with tabindex
  • 47. Dropdown menu <sigh /> var dropdown = { init: function (dropdown) { dropdown.onmouseover = this.mouseOver; dropdown.onmouseout = this.mouseOut; var x = dropdown.getElementsByTagName('li'); for (var i=0;i<x.length;i++) { x[i].onfocus = this.mouseOver; x[i].onblur = this.mouseOut; } } }
  • 48. Dropdown menu <sigh /> 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; } } }
  • 49. Dropdown menu <sigh /> 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; } } }
  • 50. Event delegation So we're stuck with setting a focus and blur event on every single link. Or are we ... ? In my recent Yahoo! presentation I give an outline of the solution. http://yuiblog.com/blog/2009/04/27/video-ppk-jsevents/
  • 52. And what about click? We're in luck: click also fires when the user activates an element by keyboard. Restriction: the element must be able to receive the keyboard focus
  • 54. Separate concepts Drag-and-drop uses the mousemove event and if there's one thing that's impossible to emulate with the keyboard it's moving the mouse
  • 55. Separate concepts Drag-and-drop uses the mousemove event How do we make this keyboard accessible? By allowing the user to use the arrow keys. Key events.
  • 57. keydown When a key is depressed. Repeats. keypress keyup
  • 58. keydown When a key is depressed. Repeats. keypress When a character key is depressed. Repeats. keyup
  • 59. keydown When a key is depressed. Repeats. keypress When a character key is depressed. Repeats. keyup When a key is released.
  • 62. Originally this theory was created by Microsoft. Safari has copied it. It's the only theory; Firefox and Opera just fire some random events.
  • 63. keydown When a key is depressed. Repeats. keypress When a character key is depressed. Repeats.
  • 64. Which key did my user press? el.onkeydown = function (e) { e = e || window.event; var realKey = e.keyCode; }
  • 65. Which key did my user press? el.onkeydown = function (e) { e = e || window.event; var realKey = e.keyCode; }
  • 66. Separate concepts Back to the drag-and-drop
  • 67. Separate concepts Drag-and-drop We need the keydown event, because arrow keys are special keys.
  • 69. Separate concepts Drag-and-drop obj.onmousemove = obj.onkeydown = moveElement; Doesn't work.
  • 70. Separate concepts Drag-and-drop obj.onmousemove = obj.onkeydown = moveElement; Mousemove expects mouse coordinates. The layer moves to these coordinates.
  • 71. Separate concepts Drag-and-drop obj.onmousemove = obj.onkeydown = moveElement; The key events expect a keystroke.
  • 72. obj.onkeydown = function (e) { e = e || window.event; var key = e.keyCode; switch (key) { case 37: // left case 38: // up case 39: // right case 40: // down return false; default: return true; } }
  • 73. Separate concepts But what does “user hits right arrow once” mean? 10px? 50px? “Move to next receptor element?” Something else that fits your interface?
  • 74. Separate concepts Drag-and-drop We have to program for two totally different situations. We need separate scripts. obj.onmousemove = moveByMouse; obj.onkeydown = moveByKeys;
  • 75. Separate concepts Drag-and-drop Yes, that's more work. But if you do it right you've got a generic drag and drop module you can use anywhere.
  • 76. Separate concepts Drag-and-drop Besides, I created a first draft for you. http://quirksmode.org/js/dragdrop.html
  • 77.
  • 79. 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
  • 80. - text fields - select boxes - checkboxes and radios
  • 81. - text fields - select boxes - checkboxes and radios focus blur No change event. The value hasn't been modified.
  • 82. - text fields - select boxes - checkboxes and radios focus blur Change event. The value has been modified.
  • 83. - text fields - select boxes - checkboxes and radios Mouse: Click on select
  • 84. - text fields - select boxes - checkboxes and radios Mouse: Click on new option CHANGE
  • 85. - text fields - select boxes - checkboxes and radios Keyboard: focus Focus on select
  • 86. - text fields - select boxes - checkboxes and radios Keyboard: focus arrow Arrow keys to move to other option CHANGE
  • 87. - text fields - select boxes - checkboxes and radios This is a BUG! Arrow keys to move to other option CHANGE
  • 88. - text fields - select boxes - checkboxes and radios Keyboard: focus arrow Arrow keys to move to other option
  • 89. - text fields - select boxes - checkboxes and radios Keyboard: focus arrow blur Blur select box. CHANGE
  • 90. - text fields - select boxes - checkboxes and radios click CHANGE when the checked property changes.
  • 91. - text fields - select boxes - checkboxes and radios click ...
  • 92. - text fields - select boxes - checkboxes and radios click blur CHANGE when the element loses the focus.
  • 93. - text fields - select boxes - checkboxes and radios This is a BUG! CHANGE when the element loses the focus.
  • 95. Questions? Ask away. Or ask me on Twitter http://twitter.com/ppk or on my site http://quirksmode.org