Top Banner
The principles of unobtrusive JavaScript Peter-Paul Koch (ppk) http://quirksmode.org An Event Apart Boston, June 24th, 2008 Hell is other browsers - Sartre
81

The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk aea boston 2008-06-24

Jul 20, 2016

Download

Documents

Unobtrusive JavaScript is not a technique.
Unobtrusive JavaScript is more like a philosophy for using JavaScript in its context:
Usable, Accessible, Standards-Compliant Web Pages
Two Fundamental Princples of Unobtrusive JavaScript:
1) Separation of Structure, Presentation, and Behavior
- Separate Them
- Connect Them (via Hooks)
2) The Script Doesn't Assume Anything
- "JavaScript is Always Available"
- "Everybody Uses a Mouse"

Unobtrusive JavaScript: it's really not that hard.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

The principles of unobtrusive JavaScript

Peter-Paul Koch (ppk)http://quirksmode.org

An Event Apart Boston, June 24th, 2008

Hell is other browsers - Sartre

Page 2: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Wikipedia: “an emerging paradigm in the JavaScript programming language.”

Me:it's just a good idea.

Page 3: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

It's not a technique

It's more like a philosophyfor using JavaScript in its context:

usable, accessible, standards-compliant web pages

Page 4: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything

Page 5: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything

Page 6: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior - Separate them - Connect them

Page 7: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior - Separate them - Connect them

Page 8: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Separation of HTML and CSS:

<div style=”position: relative”>

Page 9: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Separation of HTML and CSS:

<div style=”position: relative”>

No inline styles!

Page 10: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Separation of HTML and CSS:

<div class=”container”>

div.container {position: relative;

}

Page 11: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Separation of HTML and JavaScript:

<input onmouseover=”doSomething()” />

Page 12: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Separation of HTML and JavaScript:

<input onmouseover=”doSomething()” />

No inline event handlers!

Page 13: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Separation of HTML and JavaScript:

<input id=”special” />

$('special').onmouseover = function () {

doSomething();}

Page 14: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Advantages

- Ease of maintenance

Page 15: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Separation of HTML and JavaScript:

<input id=”special” />

$('special').onmouseover = function () {

doSomething();}

Page 16: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Separation of HTML and JavaScript:

<input id=”special” />

$('special').onmouseover = $('special').onfocus = function () {

doSomething();}

Page 17: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate them

Advantages

- Ease of maintenance- The CSS and JavaScript layers can be edited simultaneously

Page 18: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior - Separate them - Connect them

Page 19: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior - Separate them - Connect them

Page 20: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

Hooks

Page 21: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

- id

document.getElementById();

Page 22: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

- id

document.getElementById('special').onmouseover = doSomething;

Page 23: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

- id

var el = document.getElementById('special');if (el) {

el.onmouseover = doSomething;}

“Is this element available?”

Page 24: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

- id- class

getElementsByClassName();or a library function

Page 25: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

- id- class

var els = document.getElementsByClassName('special')if (els.length) {

// go through all elements and do something}

Page 26: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

- id- class

Use the same hook for presentation and behavior; for CSS and JavaScript.

Page 27: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

<ol class=”dropdown“>

Now what would this <ol> be?

Surprise:it's a dropdown menu

Page 28: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

<ol class=”dropdown“>

The class is a hook for both layers.

ol.dropdown {// presentation layer

}

Page 29: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Connect them

<ol class=”dropdown“>

The class is a hook for both layers.

var dropdowns = $('dropdown');if (dropdowns.length) {

// initialize behavior layer}

Page 30: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior - Separate them - Connect them

Page 31: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything

Page 32: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything

Page 33: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24
Page 34: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything

- “JavaScript is always available” - “Everybody uses a mouse”

Page 35: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything

- “JavaScript is always available” - “Everybody uses a mouse”

Page 36: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

JavaScript is always available

Nonsense!

Page 37: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

JavaScript is always available

- Primitive cell phones don't support it (sufficiently)

- Speech browsers' support may be spotty

- Company networks may filter out <script> tags

Page 38: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

JavaScript is always available

Make sure that the content and navigation of the site can be used without JavaScript.

Page 39: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

JavaScript is always available

Make sure that the content and navigation of the site can be used without JavaScript.

The page will remain accessible in all circumstances.

Page 40: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

JavaScript is always available

Make sure that the content and navigation of the site can be used without JavaScript.

You can use JavaScript for nice extras, though.

Page 41: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

JavaScript is always available

However...

Without JavaScript the page will become less user-friendly.

Can't be helped.

Page 42: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

JavaScript is always available

However...

Without JavaScript the page will become less user-friendly.

After all, the purpose of JavaScript is to add interactivity to a page.

Page 43: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything

- “JavaScript is always available” - “Everybody uses a mouse”

Page 44: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything

- “JavaScript is always available” - “Everybody uses a mouse”

Page 45: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Everybody uses a mouse

Nonsense!

Page 46: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Device independence

Page 47: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Take a dropdown menu:

var dropdown = {setEventHandlers: function (obj) {

obj.onmouseover = this.over;obj.onmouseout = this.out;

},over: function () {

// code},// etc

}

Page 48: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

It doesn't work without a mouse.

var dropdown = {setEventHandlers: function (obj) {

obj.onmouseover = this.over;obj.onmouseout = this.out;

},over: function () {

// code},// etc

}

Page 49: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

var dropdown = {setEventHandlers: function (obj) {

obj.onmouseover = this.over;obj.onmouseout = this.out;

},over: function () {

// code},// etc

}

We need evens that are fired when the user “enters” or “leaves” a link by using the keyboard.

focus and blur

Page 50: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

var dropdown = {setEventHandlers: function (obj) {

obj.onmouseover = obj.onfocus = this.over;obj.onmouseout = obj.onblur = this.out;

},over: function () {

// code},// etc

}

Page 51: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

var dropdown = {setEventHandlers: function (obj) {

obj.onmouseover = obj.onfocus = this.over;obj.onmouseout = obj.onblur = this.out;

},over: function () {

// code},// etc

}

Restriction: the object must be able to gain the keyboard focus.

- links- form fields

Page 52: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

var dropdown = {setEventHandlers: function (obj) {

obj.onmouseover = obj.onfocus = this.over;obj.onmouseout = obj.onblur = this.out;

},over: function () {

// code},// etc

}

Restriction: the object must be able to gain the keyboard focus.

- links- form fields- elements with tabindex

Page 53: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

And what about click?

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

click should be called activate.

Page 54: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

And what about click?

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

Restriction: the object must be able to gain the keyboard focus.

Page 55: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Click as activate

arrow.onclick = showMenu;

Page 56: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Click as activate

arrow.onclick = showMenu;

1) Mouse click on the arrow

Page 57: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Click as activate

arrow.onclick = showMenu;

1) Mouse click on the arrow2) a) Keyboard focus on the arrow

Page 58: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Click as activate

arrow.onclick = showMenu;

1) Mouse click on the arrow2) a) Keyboard focus on the arrow b) Space bar on the arrow

That's two actions.

Page 59: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Click as activate

arrow.onclick = arrow.onfocus = showMenu;

1) Mouse click on the arrow2) Keyboard focus on the arrow b) Space bar on the arrow

Page 60: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Click as activate

arrow.onclick = arrow.onfocus = showMenu;

1) Mouse click on the arrow2) Keyboard focus on the arrow

The next tab will focus on the sub-menu. The user won't be able to skip it.

Page 61: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Click as activate

arrow.onclick = arrow.onfocus = showMenu;

Generally, keyboard users need more actions to achieve the same goals as mouse users.

Don't interfere too much. There are reasons for this behavior, and keyboard users are used to it.

Page 62: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-dropuses the mousemove event

Page 63: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-dropuses the mousemove event

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

it's moving the mouse

Page 64: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-dropuses the mousemove event

How do we make this accessible?

By allowing the user to use the arrow keys.Key events.

Page 65: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

For detecting arrow keys (or other special keys) we need the keydown event.

Not keypress. (Doesn't work in IE and Safari)

Page 66: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

For detecting arrow keys (or other special keys) we need the keydown event.

Not keypress. (Doesn't work in IE and Safari)

Page 67: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

obj.onmousemove = obj.onkeydown = moveElement;

Page 68: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

obj.onmousemove = obj.onkeydown = moveElement;

Doesn't work.

Page 69: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

obj.onmousemove = obj.onkeydown = moveElement;

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

Page 70: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

obj.onmousemove = obj.onkeydown = moveElement;

The key events expect a keystroke.

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

Page 71: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

obj.onmousemove = obj.onkeydown = moveElement;

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

Page 72: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

obj.onmousemove = obj.onkeydown = moveElement;

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

Page 73: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

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

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

Page 74: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

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

Yes, that's more work.

Page 75: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

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

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

Page 76: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

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

Besides, I created a first draft for you.

Page 77: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Separate concepts

Drag-and-drop

http://quirksmode.org/js/dragdrop.html

Besides, I created a first draft for you.

Page 78: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScript

Two fundamental principles:

1) Separation of structure, presentation, and behavior 2) The script doesn't assume anything.

Page 79: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Unobtrusive JavaScriptIt's not that hard

Page 80: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Need help?

Chris Heilmann:http://onlinetools.org/articles/unobtrusivejavascript/http://icant.co.uk/articles/seven-rules-of-unobtrusive-javascript/

Jeremy Keith:http://www.alistapart.com/articles/behavioralseparation

and of course quirksmode.org

Page 81: The Principles of Unonbtrusive JavaSciprt "Hell is other browsers" - Sarte ppk  aea boston 2008-06-24

Questions?