Pragmatic Progressive Enhancement

98
Progressive Enhancement Christian Heilmann, AKQA Brown Bag, London, May 2008 Playing it safe in unknown territory

description

An introduction to the why and how of progressive enhancement, given as a brownbag at AKQA UK

Transcript of Pragmatic Progressive Enhancement

Page 1: Pragmatic Progressive Enhancement

Progressive Enhancement

Christian Heilmann, AKQA Brown Bag, London, May 2008

Playing it safe in unknown territory

Page 2: Pragmatic Progressive Enhancement

As web developers, we are working with the unknown.

Page 3: Pragmatic Progressive Enhancement

Right from the start the web was meant to be independent of display unit and rendering

device.

Page 4: Pragmatic Progressive Enhancement

This is the reason why web standards are so low level

technologies.

Page 5: Pragmatic Progressive Enhancement

There is not much sophistication in HTML – but a

lot of portability.

Page 6: Pragmatic Progressive Enhancement

Simplicity means sturdiness – not much can break.

Page 7: Pragmatic Progressive Enhancement

Simplicity also means easy adaptation to environments.

Page 8: Pragmatic Progressive Enhancement

There is hardly any more hostile environment than the

web.

Page 9: Pragmatic Progressive Enhancement

You have no clue whatsoever about the technology, the

ability or the personal settings of the end user.

Page 10: Pragmatic Progressive Enhancement

If you embrace this fact, you are ready to become a good

developer.

Page 11: Pragmatic Progressive Enhancement

Good developers are lazy – they don’t want to do the

same thing twice.

Page 12: Pragmatic Progressive Enhancement

Instead, we find solutions that do the job for us.

Page 13: Pragmatic Progressive Enhancement

One of these is progressive enhancement.

Page 14: Pragmatic Progressive Enhancement

Instead of building a solution based on assumptions...

Page 15: Pragmatic Progressive Enhancement

“Everybody has JavaScript enabled and a fast computer”

Page 16: Pragmatic Progressive Enhancement

“All our users have a resolution of 1280 x 1024”

Page 17: Pragmatic Progressive Enhancement

“Yeah, 40 menu entries are OK, there is enough space on

the screen”

Page 18: Pragmatic Progressive Enhancement

... we work in a paranoid fashion.

Page 19: Pragmatic Progressive Enhancement

Things *will* break and the person who created the

interface will get the blame.

Page 20: Pragmatic Progressive Enhancement

Does this mean we need to dumb down our solutions?

Page 21: Pragmatic Progressive Enhancement

Does it mean JavaScript and Flash and Silverlight are evil?

Page 22: Pragmatic Progressive Enhancement

Only if we built them evil.

Page 23: Pragmatic Progressive Enhancement

Or if we build them badly.

Page 24: Pragmatic Progressive Enhancement

Used the correct way, JavaScript can help you build

amazingly adaptive and clever applications.

Page 25: Pragmatic Progressive Enhancement

Seven steps to take to enhance progressively:

Page 26: Pragmatic Progressive Enhancement

1. Separate as much as possible.

2. Build on things that work

3. Generate dependent markup

4. Test for everything before you apply it.

5. Explore the environment

6. Load on demand

7. Modularize code

Page 27: Pragmatic Progressive Enhancement

Let’s go through them in detail...

Page 28: Pragmatic Progressive Enhancement

1. Separate as much as possible.

Page 29: Pragmatic Progressive Enhancement

Separation of structure (HTML), presentation (CSS)

and behaviour (JS) is an absolute must.

Page 30: Pragmatic Progressive Enhancement

The reason is pragmatism.

Page 31: Pragmatic Progressive Enhancement

You leave it up to the browser to apply what it can support.

Page 32: Pragmatic Progressive Enhancement

You also know exactly where to change what

(and you will have to change things constantly)

Page 33: Pragmatic Progressive Enhancement

2. Build on things that work

Page 34: Pragmatic Progressive Enhancement

Using JavaScript, you can make anything interactive.

Page 35: Pragmatic Progressive Enhancement

Using CSS, you can make anything look like something

it isn’t.

Page 36: Pragmatic Progressive Enhancement

With great power comes great responsibility.

Page 37: Pragmatic Progressive Enhancement

You simulate interaction that is most likely a lot more complex than you think.

Page 38: Pragmatic Progressive Enhancement

Interactive elements can be used with a lot of different

input devices.

Page 39: Pragmatic Progressive Enhancement

They also report to all kind of systems when activated and trigger appropriate actions.

Page 40: Pragmatic Progressive Enhancement

If you simulate, do it right – don’t break expectations.

Page 41: Pragmatic Progressive Enhancement

Users trust you to give them a working system – this is what

they came for.

Page 42: Pragmatic Progressive Enhancement

Best to use what works – links, forms and buttons.

Page 43: Pragmatic Progressive Enhancement

<span onclick=”help()”>Help</span>

Page 44: Pragmatic Progressive Enhancement

<a href="/help" id="help">Help</a><script>YAHOO.example.helpdemo = function(){ function doHelp(){ // other work here... } YAHOO.util.Event.on('help','click',function(e){ doHelp(); YAHOO.util.Event.preventDefault(e); });}();</script>

Page 45: Pragmatic Progressive Enhancement

3. Generate dependent markup

Page 46: Pragmatic Progressive Enhancement

If your script needs HTML that only makes sense when being

driven or changed by JavaScript, generate it with

JavaScript.

Page 47: Pragmatic Progressive Enhancement

Examples are window.print() links or anything that pulls in

data only when JS is available.

Page 48: Pragmatic Progressive Enhancement

Using the DOM you can create whatever you need.

Page 49: Pragmatic Progressive Enhancement

4. Test for everything before you apply it.

Page 50: Pragmatic Progressive Enhancement

This is a no-brainer, as you cannot trust circumstances.

Page 51: Pragmatic Progressive Enhancement

Remember “check the depth of the pool before you jump

in head-first”?

Page 52: Pragmatic Progressive Enhancement

<script type=”text/javascript”> var c = document.getElementById(‘info’); var text = document.createTextNode(message); c.appendChild(text);</script>

Page 53: Pragmatic Progressive Enhancement

You cannot modify properties of the unknown!

Page 54: Pragmatic Progressive Enhancement

<script type=”text/javascript”> var c = document.getElementById(‘info’); if(c){ var text = document.createTextNode(message); c.appendChild(text); }</script>

Page 55: Pragmatic Progressive Enhancement

The same applies to any browser or DOM method or attribute you try to access.

Page 56: Pragmatic Progressive Enhancement

JavaScript has conditions.

Use them.

Page 57: Pragmatic Progressive Enhancement

5. Explore the environment

Page 58: Pragmatic Progressive Enhancement

Browsers tell you a lot about themselves when you use JavaScript and the DOM.

Page 59: Pragmatic Progressive Enhancement

You get the browser viewport size, how far down the

document the user scrolled, where the mouse pointer is

and much more.

Page 60: Pragmatic Progressive Enhancement

You can use this to make sure things don’t break.

Page 61: Pragmatic Progressive Enhancement

For example menus or panels.

Page 62: Pragmatic Progressive Enhancement
Page 63: Pragmatic Progressive Enhancement
Page 64: Pragmatic Progressive Enhancement
Page 65: Pragmatic Progressive Enhancement
Page 66: Pragmatic Progressive Enhancement
Page 67: Pragmatic Progressive Enhancement
Page 68: Pragmatic Progressive Enhancement
Page 69: Pragmatic Progressive Enhancement

6. Load on demand

Page 70: Pragmatic Progressive Enhancement

One of the biggest issues on the web is performance.

Page 71: Pragmatic Progressive Enhancement

Things are never fast enough.

Page 72: Pragmatic Progressive Enhancement

Therefore it is a good idea to load extraneous content and

code needed to support it only when you need to.

Page 73: Pragmatic Progressive Enhancement

You can for example test which images are visible (by

comparing their location with the viewport) and only load

them when they are.

Page 74: Pragmatic Progressive Enhancement

You can also start with a really small script that

generates script nodes with the DOM to pull in larger

library code when and if it is needed and can be applied.

Page 75: Pragmatic Progressive Enhancement

7. Modularize code

Page 76: Pragmatic Progressive Enhancement

This happens in several ways.

Page 77: Pragmatic Progressive Enhancement

First of all you want to ensure that your code does work

nicely with other code.

Page 78: Pragmatic Progressive Enhancement

The easiest way to do that is the module pattern.

Page 79: Pragmatic Progressive Enhancement

<script type=”text/javascript”> function init(){ // initialization } function show(){ // show stuff } init();</script>

Page 80: Pragmatic Progressive Enhancement

<script type=”text/javascript”> modules = function(){ function init(){ // initialization } function show(){ // show stuff } init(); }();</script>

Page 81: Pragmatic Progressive Enhancement

<script type=”text/javascript”> modules = function(){ function init(){ // initialization } function show(){ // show stuff } init(); return { init:init, show:show } }(); // modules.init(); // modules.show();</script>

Page 82: Pragmatic Progressive Enhancement

<script type=”text/javascript”> (function(){ function init(){ // initialization } function show(){ // show stuff } init(); })();</script>

Page 83: Pragmatic Progressive Enhancement

The other kind of modularization that makes

sense is collating scripts and CSS definitions into different

files.

Page 84: Pragmatic Progressive Enhancement

This eases maintenance and also helps loading on

demand.

Page 85: Pragmatic Progressive Enhancement

It is not good for performance, but techniques to work around that issue is

another talk :)

Page 86: Pragmatic Progressive Enhancement

Now you know the tricks and their rationale and you have

a choice.

Page 87: Pragmatic Progressive Enhancement

You can start using them with your own code and enter a world of pain and misery

called cross-browser issues.

Page 88: Pragmatic Progressive Enhancement

Or you can use a JavaScript library and CSS framework

that does it for you.

Page 89: Pragmatic Progressive Enhancement

All good frameworks and libraries want to do two

things:

Page 90: Pragmatic Progressive Enhancement

Make your life a lot easier by normalizing browser

behaviour.

Page 91: Pragmatic Progressive Enhancement

and

bridge the gap until browsers are good and do what we tell

them to do.

Page 92: Pragmatic Progressive Enhancement

One of those libraries is YUI:http://developer.yahoo.com/yui/

Page 93: Pragmatic Progressive Enhancement

★ CSS Framework

★ JavaScript Utilities

★ Widget library

★ Skinable

★ Extendable and Modularized.

★ Powers Yahoo! pages

★ BSD licensed (commercial use is OK)

Page 94: Pragmatic Progressive Enhancement

Usable, progressively enhanced applications and

sites are not science fiction or “best case scenarios”.

Page 95: Pragmatic Progressive Enhancement

They are achievable by subscribing to the ideas of progressive enhancement and using the right tools.

Page 96: Pragmatic Progressive Enhancement

When we cut corners, we hurt ourselves the most.

Page 97: Pragmatic Progressive Enhancement

Taking pride in our job, targeting maintainability and

extendability will make us happier developers and give us more confidence to give

achievable estimates.

Page 98: Pragmatic Progressive Enhancement

THANKS!