Progressive Enhancement With JavaScript

76
Aaron Gustafson AN EVENT APART - 25 APRIL 2008 Progressive Enhancement with JavaScript

description

We’ve all heard the rallying cry for writing “unobtrusive” JavaScript, but that only gets you so far. To truly meet users’ needs, no matter what their device or platform, you’ll have to apply the principles of “progressive enhancement” to your client-side scripting. This session will help you get into the PE mindset and walk you through concrete examples that demonstrate PE in action.

Transcript of Progressive Enhancement With JavaScript

Page 1: Progressive Enhancement With JavaScript

Aaron Gustafson

AN EVENT APART - 25 APRIL 2008

Progressive Enhancement with JavaScript

Page 2: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

What isProgressive

Enhancement?

2

Page 3: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

ProgressiveEnhancement

vs.Graceful

Degradation

3

Page 4: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Graceful Degradation! A holdover from engineering

! Build for the latest browsers

! Test on older devices

! Happens at the end

! Accounts for few versions

! Expects a poor experience for older browsers

! Fixes major issues but little else

4

Page 5: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 5

Page 6: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 5

Page 7: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Progressive Enhancementfocuses on contentcontent

6

Page 8: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Content is why we build websites

and should beour central focus

7

Page 9: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

How does it work?

8

Page 10: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 9

Page 11: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 10

Flavorful nugget of content(semantic goodness)

Page 12: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 11

Rich, indulgent design

Page 13: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 12

Sweet interactivity(keeps it from melting in your hands)

Page 14: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 13

Page 15: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Technologies applied as layers

14

Page 16: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 15

Page 17: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 16

(Mr. Clarke’s web standards trifle)

Page 18: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 17

Why a peanut m&m?

Page 19: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

The peanut is good for

18

! diabetics

! search engine spiders

! mobile devices

Page 20: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 19

It’s a basic levelof support

Page 21: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 20

! some browsers only handle a certain level of CSS

! some companies turn o" JavaScript

Rich, chocolaty goodness

Page 22: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 21

It can be beautiful(and progressive)

Page 23: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

The candy coating

22

! JavaScript can turn a website into an experience

! we can deliver our scripts a la carte

Page 24: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 23

How do we do it?

Page 25: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Restraint& Planning

24

Page 26: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Wieldyour power

wisely

25

Page 27: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Dear George Lucas, please don’t sue me. Kthxbye.

A long time ago, in acubicle far, far away...

26

Page 28: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

DHTML

27

(was awful)

Page 29: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Spaghetti code

28

(our HTML was spaghetti too)

Page 30: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Things are better now,but we have new challenges

29

Page 31: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

What canJavaScript do?

30

Page 32: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

How shouldJavaScriptbe used?

31

JavaScript

Page 33: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Progressive Enhancementhelps us by reminding usto focus on the content

32

Page 34: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Back to basics

33

XHTML + HTTP

Page 35: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Back to basics

33

solid foundation

Page 36: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

All code neededto understandand use a site

should exist beforeany scripts run

PE Maxim:

34

Page 37: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Period.

PE Maxim:

34

Page 38: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Consider lala.com

35

Page 39: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Without JavaScript

36

Page 40: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Without JavaScript

36

Page 41: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Without JavaScript

36

Doh!

Page 42: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

And looking at the markup...

37

Page 43: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

And looking at the markup...

37

Ugh!

Page 44: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

What about search?

38

Page 45: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

What aboutalternative devices?

39

Page 46: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

EnterUnobtrusiveJavaScript

40

the cornerstone of ProgressiveEnhancement with JavaScript( )

Page 47: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Don’t do this

41

<a href="javascript:someFunction();">some text</a>

or

<a href="javascript:void(null);" onclick="someFunction();">some text</a>

or

<a href="#" onclick="someFunction();">some text</a>

Page 48: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

An improvement, but still...

42

<a href="http://offsite.com" onclick="newWin( this.href ); return false;"> some text</a>

Page 49: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

window.onload = handleExternalLinks;

function handleExternalLinks(){

var server = document.location.hostname; var anchors = document.getElementsByTagName("a"); var i, href; for( i=0; i < anchors.length; i++ ){ href = anchors[i].href;

if( href.indexOf("http://" + server) == -1 && href.indexOf("https://" + server) == -1 ){ // HREF is not a file on my server anchors[i].onclick = function(){ newWin( this.href );

}; } }}

Getting warmer...

43

Page 50: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

document.getElementsByTagName( 'body' )[0].onclick = clickDelegator;

function clickDelegator( e ){ e = ( e ) ? e : event; var el = e.target || e.srcElement;

// external links

if( el.nodeName.toLowerCase() == 'a' && el.getAttribute( 'rel' ) == 'external' ){ newWin( el.href ); }}

You’re hot

44

Page 51: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Keep the hot side hotand the cold side cold

45

or getting some separationbetween your CSS and JS( )

Page 52: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Remember stu" like this?

46

<a href="foo.html" style="color:blue;" onmouseover="this.style.color='red'"

onmouseout="this.style.color='blue'">Foo</a>

Page 53: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Remember stu" like this?

46

<a href="foo.html" style="color:blue;" onmouseover="this.style.color='red'"

onmouseout="this.style.color='blue'">Foo</a>

Obvisouly, we should be doing this instead:

a, a:link, a:visited { color: blue;}

a:hover { color: red;}

Page 54: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

CSS waspoorly understood

back then

47

Page 55: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 47

but we knowbetter now

Page 56: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 47

don’t we?

Page 57: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Hmm...

48

for( i=0; i<objects.length; i++){ objects[i].style.display = 'none';}

Page 58: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Better

49

for( i=0; i<objects.length; i++){ objects[i].style.position = 'absolute'; objects[i].style.left = '-999em';

}

Page 59: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Separation

50

CSS:

.hidden { position: absolute; left: -999em;}

JavaScript:

for( i=0; i<objects.length; i++){ objects[i].addClassName( 'hidden' );

}

Page 60: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Maintenance options

51

! external style rules added to master CSS file by hand

! external CSS file added to the document by hand

! external CSS file added to the document via script

! embedding CSS in the document via script

Page 61: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

But keep in mind

52

Page 62: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 52

you need toavoid applying

script-related styles

Page 63: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 52

until you knowyour script can run

and it hasinitialized properly

Page 64: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

How do we do that?

53

! make your style rules specific:

.TabInterface-folder {

...

}

or

#TabInterface .tab {

...

}

#TabInterface .tab.active {

...

}

Page 65: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

How do we do that?

54

! turn “on” script-related rules at run-time:

<div id="main" class="tabbed">

becomes

<div id="main" class="tabbed-on">

Page 66: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

How do we do that?

54

! turn “on” script-related rules at run-time:

Default Activated

add “-on” to the class .tabbed .tabbed-on

add an activation class .auto-submit .auto-submit.active

change the form of the class .replace-me .replaced

Page 67: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Become a detective

55

Page 68: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Look for methods

56

function someFunction(){ if( !document.getElementsByTagName ) return; // code that uses document.getElementsByTagName()

...}

Page 69: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Look for methods

57

function someFunction(){ if( !document.getElementsByTagName || !document.getElementById ) return;

/* code that uses document.getElementsByTagName() and document.getElementById() */}

Page 70: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Look for elements

58

function someFunction(){ if( !document.getElementsByTagName || !document.getElementsByTagName( 'p' ) ) return;

/* code that uses document.getElementsByTagName() and requires the presence of a P element */ ...}

Page 71: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Look for identified elements

59

function someFunction(){ if( !document.getElementById || !document.getElementById( 'content' ) ) return;

// code that requires the presence of #content ...}

Page 72: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Look for objects

60

function someFunction(){ if( typeof( Prototype ) == 'undefined' ) return; // code that uses Prototype

...}

Page 73: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Look for object versions

61

function someFunction(){ if( typeof( Prototype ) == 'undefined' || parseFloat( Prototype.Version ) < 1.5 ) return;

// code that uses Prototype 1.5 or higher ...}

Page 74: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Look before you leap

62

window.onload = function(){ if( document.getElementsByTagName && document.getElementById ){

someFunction(); }};

Page 75: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008

Be progressive

63

! Start with the content, then work your way out

! Get unobtrusive

! Keep some distance between your styles & your scripts

! Put on your detective hat

Page 76: Progressive Enhancement With JavaScript

PROGRESSIVE ENHANCEMENT WITH JAVASCRIPT

AN EVENT APART - 25 APRIL 2008 64

Questions?