SlideShare a Scribd company logo
Progressive
 Enhancement
With JavaScript
       and Ajax




                  Highland Fling 2007
                  Christian Heilmann
• As web developers we are in a
  very lucky position.
• Unlike real world products,
  web sites do not have a fixed
  state.
• Web documents were right
  from the start meant to be
  adaptive to their environment.
• By keeping things flexible and
  adaptive we can please all
  consumers.

                                   http://www.flickr.com/photos/oskay/420878465/
• Real life enhancement
• Real life has some ideas of
  progressive enhancement,
  most of which are old
  chestnuts:
  – Measure twice, cut once.
  – Check the depth of the lake
    before you plunge in.
  – Try before you buy
  – = Use Bittorrent
• The same logic applies to
  JavaScript and Ajax:
  – Test if JavaScript is available.
  – Test if the DOM is supported
  – Test for the HTML you want to
    use.
  – Test for any object you want to
    use.
  – Don’t rely on the connection to
    be available.
• Testing for JavaScript is easy,
  simply don’t make your
  product rely on JavaScript:
     – Death to href=“javascript:foo()”
     – Death to onclick=“foo()”
     – Who the hell ever told you to
       use onclick=“javascript:foo()” ?

• Don’t mix HTML and
  JavaScript, use the DOM to
  access HTML.
• The DOM is your API to the
  document.
http://www.flickr.com/photos/jungle_boy/140233674/
• Testing for DOM support is as easy:
  – Check that getElementById and is available.
  – If you still care about Opera 6 also test for createTextNode




     function init(){
       if( document.getElementById &&
           document.createTextNode )
         // we have DOM, wahey!
         checkDependencies();
       }
     }
• Testing for the HTML you need is also easy:
  – Check that all the elements are available.




    function checkDependencies(){
       var usedIds = ['nav','content'];
       var isOk = true;
       for(var i = 0; usedIds[i]!==undefined; i++) {
         if( !document.getElementById( usedIds[i] ) ){
           isOk = false;
         }
       }
       return isOk;
     }
• Testing for the HTML you need is also easy:
  – Check that all the elements are available.
  – Check that they also have the right child nodes




     function myNavStuff(){
        var n = document.getElementById('nav');
        if(n.getElementsByTagName('ul').length > 0){
          // I got your code right here!
        }
      }
• Testing for object availability
   – Simply test for any object before you access it




     var request;
     try{
       request = new XMLHttpRequest();
     }catch(error){
       try{
         request = new ActiveXObject(quot;Microsoft.XMLHTTPquot;);
       }catch(error){
         return true;
       }
     }
• Prepare for unreliable and
  flaky connections.
  – Set a timer to time out and
    abort the connection after a
    certain period has passed.
  – Define a success and failure
    case for any connection.
  – Make sure you avoid caching
    issues by setting an expired
    HTTP header on your Ajax calls
• This is the “how” of
  progressive enhancement
  with JavaScript and Ajax.
• The question however is why
  use JavaScript at all?
• After all it can be turned off.
• Instead we can use the
  amazing new trick called
  “CSS only solutions”.
• Why bother with writing
  complex scripts when you can
  do the same with some easy
  lines of CSS?
• Showing and hiding menus?
  #nav li ul { display:none }
  #nav li:hover ul {
      display:block
  }
• Using CSS for behaviour is
  based on one trick, which is
  pseudo selectors like :hover,
  :active and :focus
• These have several problems:
  – Some browsers only support
    them on interactive elements
    like links and buttons.
  – They have a strict on or off
    state.
  – and they fail for a certain user
    group…
• Keyboard access support in
  HTML and browsers is bad.
• Support for :active and :focus
  is even worse.
• Which means that CSS only
  solutions with keyboard
  support turn out to be HTML
  soup nightmares (nesting
  tables inside links…)
Progressive Enhancement with JavaScript and Ajax
• In essence, CSS only solutions
  are a bit of a shifty character
  and you don’t know if you can
  trust them.
• It is also debateable if CSS
  only solutions are in the end
  easier than JS solutions.
• JavaScript has so many more
  options for you:
  – Real testing for support of
    objects
  – Real event handling including
    simulation of application
    keyboard handling (cursors
    instead of tabbing)
  – timeout() to allow for user errors
  – Creation of necessary markup
    on demand.
• JavaScript has so many more
  options for you:
    – Test of available screen estate
    – Loading of extra content on
      demand




 http://www.flickr.com/photos/drewm/227562632/
Progressive Enhancement with JavaScript and Ajax
• Does this mean that only
  scripters should create
  interfaces and CSS is not
  needed?
• Not at all, as first of all CSS
  can help JavaScript a lot.




http://www.flickr.com/photos/drewm/225911999/in/set-507727/
• Task: Hide all list items in the first UL
   – A lot of code, that one…




 window.onload = function(){
   if(document.getElementById && document.createTextNode){
     var uls = document.getElementsByTagName('ul');
     if(uls.length > 0){
       var lis = uls[0].getElementsByTagName('li');
       for(var i = 0, j = lis.length; i < j; i++){
         lis[i].style.display = 'none';
       }
     }
   }
 }
• Task: Hide all list items in the first UL
   – How about leaving the main job to the CSS parser?




window.onload = function(){
  if(document.getElementById && document.createTextNode){
    var b = document.body;
    b.className += b.className ? ' dynamic' : 'dynamic';
  }
}
<style type=quot;text/cssquot;>
  body.dynamic ul li{
    display:none;
  }
</style>
• This also allows you to keep
  the whole look and feel of
  your scripts in the hands of
  the CSS maintainers.
• All you need to do is make
  sure you communicate the
  class and ID names with
  them.
• You achieve highest flexibility
  by separating those and the
  text labels out into own
  includes:
   slideshow.js
   slideshow-css.js
   slideshow-labels.js

      slideshow.css = {

      'navigationID':'nav',
        'contentID':'main',
        'hide class':'hide'
      }
• There is a danger in
  progressive enhancement
  with JavaScript though:
• Be aware that what you
  enhance is as important as
  how you enhance it.
• A solution that doesn’t really
  make sense shouldn’t be
  enhanced, but changed.
• Useless progressive
  enhancement.




    Pointless name dropping

      Get on with it…
• It is important to know your
  audience
• It is important to keep up with
  technology as a lot of truths
  of the past are not applicable
  any longer.
• Research what can be
  enhanced and how.
• Example “connected select
  boxes”:
• Research what can be
  enhanced and how.
• Example “hcard to
  Googlemap”
• So why isn’t progressive
  enhancement yet a common
  practice?




http://www.flickr.com/photos/urlgirl/199044322/
• Stockholm Syndrome
• Stockholm Syndrome
• Enhanced interfaces don’t go
  down too well with the
  mainstream audience, we are
  used to waiting and things
  breaking.
• Technologies are perceived as
  given, no need to improve
  them.
• Monetary reasons
• Progressive Enhancement
  costs money
• As with any best practice
  there is no immediate
  tangible benefit
• Show desire
• You can help to change this
• Look at things, and see how
  you can improve them
• If need be, do it on the sly
  (hcard in Yahoo! Local)
• Benefit from experience of
  others
• A lot of tricks that helped us
  in the past can be brushed up
  and renewed now.
• Example “lowsrc”
• Keep your feelers out there.
• Read up on technologies and
  what is done with them.
• Hot:
  – http://www.w3.org/TR/aria-
    roadmap/
  – http://www.whatwg.org/
• Contact, subscribe, talk,
  share.
• If you know something, please
  tell others:
  http://makemeaspeaker.com
• Good progressive
  enhancement helps the user,
  and not the developer.
     – Automatic saving of drafts in
       email apps
     – Parallel upload of content while
       you can add meta data
     – Making it easy and fun to add
       content.
     – Preventing unnecessary steps.




http://www.flickr.com/photos/radiofree/286343390/
THANKS!




chris.heilmann@gmail.com
http://wait-till-i.com
http://icant.co.uk
Flickr/delicious: codepo8

More Related Content

What's hot

Why and How to Use Virtual DOM
Why and How to Use Virtual DOMWhy and How to Use Virtual DOM
Why and How to Use Virtual DOM
Daiwei Lu
 
JavaScript DOM Manipulations
JavaScript DOM ManipulationsJavaScript DOM Manipulations
JavaScript DOM Manipulations
Ynon Perek
 
jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009
Ralph Whitbeck
 
JavaScript
JavaScriptJavaScript
JavaScript
tutorialsruby
 
Taiwan Web Standards Talk 2011
Taiwan Web Standards Talk 2011Taiwan Web Standards Talk 2011
Taiwan Web Standards Talk 2011
Zi Bin Cheah
 
Javascript and DOM
Javascript and DOMJavascript and DOM
Javascript and DOM
Brian Moschel
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
Dave Artz
 
10 java script projects full source code
10 java script projects full source code10 java script projects full source code
10 java script projects full source code
Laurence Svekis ✔
 
Google’s PRPL Web development pattern
Google’s PRPL Web development patternGoogle’s PRPL Web development pattern
Google’s PRPL Web development pattern
Jeongkyu Shin
 
Backbone
BackboneBackbone
Backbone
Ynon Perek
 
JavaScript and BOM events
JavaScript and BOM eventsJavaScript and BOM events
JavaScript and BOM events
Jussi Pohjolainen
 
StHack 2014 - Mario "@0x6D6172696F" Heiderich - JSMVCOMFG
StHack 2014 - Mario "@0x6D6172696F" Heiderich - JSMVCOMFGStHack 2014 - Mario "@0x6D6172696F" Heiderich - JSMVCOMFG
StHack 2014 - Mario "@0x6D6172696F" Heiderich - JSMVCOMFG
StHack
 
JavaScript Libraries (@Media)
JavaScript Libraries (@Media)JavaScript Libraries (@Media)
JavaScript Libraries (@Media)
jeresig
 
The Complementarity of React and Web Components
The Complementarity of React and Web ComponentsThe Complementarity of React and Web Components
The Complementarity of React and Web Components
Andrew Rota
 
The Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event HandlingThe Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event Handling
Yorick Phoenix
 
Vue.js for beginners
Vue.js for beginnersVue.js for beginners
Vue.js for beginners
Julio Bitencourt
 
JavaScript Libraries (Kings of Code)
JavaScript Libraries (Kings of Code)JavaScript Libraries (Kings of Code)
JavaScript Libraries (Kings of Code)
jeresig
 
1 ppt-ajax with-j_query
1 ppt-ajax with-j_query1 ppt-ajax with-j_query
1 ppt-ajax with-j_query
Fajar Baskoro
 
When dynamic becomes static - the next step in web caching techniques
When dynamic becomes static - the next step in web caching techniquesWhen dynamic becomes static - the next step in web caching techniques
When dynamic becomes static - the next step in web caching techniques
Wim Godden
 
jQuery and the W3C
jQuery and the W3CjQuery and the W3C
jQuery and the W3C
jeresig
 

What's hot (20)

Why and How to Use Virtual DOM
Why and How to Use Virtual DOMWhy and How to Use Virtual DOM
Why and How to Use Virtual DOM
 
JavaScript DOM Manipulations
JavaScript DOM ManipulationsJavaScript DOM Manipulations
JavaScript DOM Manipulations
 
jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Taiwan Web Standards Talk 2011
Taiwan Web Standards Talk 2011Taiwan Web Standards Talk 2011
Taiwan Web Standards Talk 2011
 
Javascript and DOM
Javascript and DOMJavascript and DOM
Javascript and DOM
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
 
10 java script projects full source code
10 java script projects full source code10 java script projects full source code
10 java script projects full source code
 
Google’s PRPL Web development pattern
Google’s PRPL Web development patternGoogle’s PRPL Web development pattern
Google’s PRPL Web development pattern
 
Backbone
BackboneBackbone
Backbone
 
JavaScript and BOM events
JavaScript and BOM eventsJavaScript and BOM events
JavaScript and BOM events
 
StHack 2014 - Mario "@0x6D6172696F" Heiderich - JSMVCOMFG
StHack 2014 - Mario "@0x6D6172696F" Heiderich - JSMVCOMFGStHack 2014 - Mario "@0x6D6172696F" Heiderich - JSMVCOMFG
StHack 2014 - Mario "@0x6D6172696F" Heiderich - JSMVCOMFG
 
JavaScript Libraries (@Media)
JavaScript Libraries (@Media)JavaScript Libraries (@Media)
JavaScript Libraries (@Media)
 
The Complementarity of React and Web Components
The Complementarity of React and Web ComponentsThe Complementarity of React and Web Components
The Complementarity of React and Web Components
 
The Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event HandlingThe Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event Handling
 
Vue.js for beginners
Vue.js for beginnersVue.js for beginners
Vue.js for beginners
 
JavaScript Libraries (Kings of Code)
JavaScript Libraries (Kings of Code)JavaScript Libraries (Kings of Code)
JavaScript Libraries (Kings of Code)
 
1 ppt-ajax with-j_query
1 ppt-ajax with-j_query1 ppt-ajax with-j_query
1 ppt-ajax with-j_query
 
When dynamic becomes static - the next step in web caching techniques
When dynamic becomes static - the next step in web caching techniquesWhen dynamic becomes static - the next step in web caching techniques
When dynamic becomes static - the next step in web caching techniques
 
jQuery and the W3C
jQuery and the W3CjQuery and the W3C
jQuery and the W3C
 

Similar to Progressive Enhancement with JavaScript and Ajax

Seven Reasons for Code Bloat
Seven Reasons for Code BloatSeven Reasons for Code Bloat
Seven Reasons for Code Bloat
Christian Heilmann
 
Building a JavaScript Library
Building a JavaScript LibraryBuilding a JavaScript Library
Building a JavaScript Library
jeresig
 
How to make Ajax Libraries work for you
How to make Ajax Libraries work for youHow to make Ajax Libraries work for you
How to make Ajax Libraries work for you
Simon Willison
 
About Best friends - HTML, CSS and JS
About Best friends - HTML, CSS and JSAbout Best friends - HTML, CSS and JS
About Best friends - HTML, CSS and JS
Naga Harish M
 
The road to professional web development
The road to professional web developmentThe road to professional web development
The road to professional web development
Christian Heilmann
 
Testing ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NETTesting ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NET
Ben Hall
 
Automated Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend Testing
Neil Crosby
 
Learning jQuery @ MIT
Learning jQuery @ MITLearning jQuery @ MIT
Learning jQuery @ MIT
jeresig
 
Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQuery
DanWooster1
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone Interactivity
Eric Steele
 
How I learned to stop worrying and love embedding JavaScript
How I learned to stop worrying and love embedding JavaScriptHow I learned to stop worrying and love embedding JavaScript
How I learned to stop worrying and love embedding JavaScript
Kevin Read
 
Embedding V8 in Android apps with Ejecta-V8
Embedding V8 in Android apps with Ejecta-V8Embedding V8 in Android apps with Ejecta-V8
Embedding V8 in Android apps with Ejecta-V8
Kevin Read
 
Shifting Gears
Shifting GearsShifting Gears
Shifting Gears
Christian Heilmann
 
Intro To Django
Intro To DjangoIntro To Django
Intro To Django
Udi Bauman
 
jQuery Features to Avoid
jQuery Features to AvoidjQuery Features to Avoid
jQuery Features to Avoid
dmethvin
 
WebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
WebTest - Efficient Functional Web Testing with HtmlUnit and BeyondWebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
WebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
mguillem
 
JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4
alexsaves
 
GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale
GDD Japan 2009 - Designing OpenSocial Apps For Speed and ScaleGDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale
GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale
Patrick Chanezon
 
Usability in the GeoWeb
Usability in the GeoWebUsability in the GeoWeb
Usability in the GeoWeb
Dave Bouwman
 
Eugene Andruszczenko: jQuery
Eugene Andruszczenko: jQueryEugene Andruszczenko: jQuery
Eugene Andruszczenko: jQuery
Refresh Events
 

Similar to Progressive Enhancement with JavaScript and Ajax (20)

Seven Reasons for Code Bloat
Seven Reasons for Code BloatSeven Reasons for Code Bloat
Seven Reasons for Code Bloat
 
Building a JavaScript Library
Building a JavaScript LibraryBuilding a JavaScript Library
Building a JavaScript Library
 
How to make Ajax Libraries work for you
How to make Ajax Libraries work for youHow to make Ajax Libraries work for you
How to make Ajax Libraries work for you
 
About Best friends - HTML, CSS and JS
About Best friends - HTML, CSS and JSAbout Best friends - HTML, CSS and JS
About Best friends - HTML, CSS and JS
 
The road to professional web development
The road to professional web developmentThe road to professional web development
The road to professional web development
 
Testing ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NETTesting ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NET
 
Automated Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend Testing
 
Learning jQuery @ MIT
Learning jQuery @ MITLearning jQuery @ MIT
Learning jQuery @ MIT
 
Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQuery
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone Interactivity
 
How I learned to stop worrying and love embedding JavaScript
How I learned to stop worrying and love embedding JavaScriptHow I learned to stop worrying and love embedding JavaScript
How I learned to stop worrying and love embedding JavaScript
 
Embedding V8 in Android apps with Ejecta-V8
Embedding V8 in Android apps with Ejecta-V8Embedding V8 in Android apps with Ejecta-V8
Embedding V8 in Android apps with Ejecta-V8
 
Shifting Gears
Shifting GearsShifting Gears
Shifting Gears
 
Intro To Django
Intro To DjangoIntro To Django
Intro To Django
 
jQuery Features to Avoid
jQuery Features to AvoidjQuery Features to Avoid
jQuery Features to Avoid
 
WebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
WebTest - Efficient Functional Web Testing with HtmlUnit and BeyondWebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
WebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
 
JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4
 
GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale
GDD Japan 2009 - Designing OpenSocial Apps For Speed and ScaleGDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale
GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale
 
Usability in the GeoWeb
Usability in the GeoWebUsability in the GeoWeb
Usability in the GeoWeb
 
Eugene Andruszczenko: jQuery
Eugene Andruszczenko: jQueryEugene Andruszczenko: jQuery
Eugene Andruszczenko: jQuery
 

More from Christian Heilmann

Develop, Debug, Learn? - Dotjs2019
Develop, Debug, Learn? - Dotjs2019Develop, Debug, Learn? - Dotjs2019
Develop, Debug, Learn? - Dotjs2019
Christian Heilmann
 
Hinting at a better web
Hinting at a better webHinting at a better web
Hinting at a better web
Christian Heilmann
 
Taking the "vile" out of privilege
Taking the "vile" out of privilegeTaking the "vile" out of privilege
Taking the "vile" out of privilege
Christian Heilmann
 
Seven ways to be a happier JavaScript developer - NDC Oslo
Seven ways to be a happier JavaScript developer - NDC OsloSeven ways to be a happier JavaScript developer - NDC Oslo
Seven ways to be a happier JavaScript developer - NDC Oslo
Christian Heilmann
 
Artificial intelligence for humans… #AIDC2018 keynote
Artificial intelligence for humans… #AIDC2018 keynoteArtificial intelligence for humans… #AIDC2018 keynote
Artificial intelligence for humans… #AIDC2018 keynote
Christian Heilmann
 
Killing the golden calf of coding - We are Developers keynote
Killing the golden calf of coding - We are Developers keynoteKilling the golden calf of coding - We are Developers keynote
Killing the golden calf of coding - We are Developers keynote
Christian Heilmann
 
Progressive Web Apps - Techdays Finland
Progressive Web Apps - Techdays FinlandProgressive Web Apps - Techdays Finland
Progressive Web Apps - Techdays Finland
Christian Heilmann
 
Taking the "vile" out of privilege
Taking the "vile" out of privilegeTaking the "vile" out of privilege
Taking the "vile" out of privilege
Christian Heilmann
 
Five ways to be a happier JavaScript developer
Five ways to be a happier JavaScript developerFive ways to be a happier JavaScript developer
Five ways to be a happier JavaScript developer
Christian Heilmann
 
Taking the P out of PWA
Taking the P out of PWATaking the P out of PWA
Taking the P out of PWA
Christian Heilmann
 
Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"
Christian Heilmann
 
You learned JavaScript - now what?
You learned JavaScript - now what?You learned JavaScript - now what?
You learned JavaScript - now what?
Christian Heilmann
 
Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"
Christian Heilmann
 
Progressive Web Apps - Covering the best of both worlds - DevReach
Progressive Web Apps - Covering the best of both worlds - DevReachProgressive Web Apps - Covering the best of both worlds - DevReach
Progressive Web Apps - Covering the best of both worlds - DevReach
Christian Heilmann
 
Progressive Web Apps - Covering the best of both worlds
Progressive Web Apps - Covering the best of both worldsProgressive Web Apps - Covering the best of both worlds
Progressive Web Apps - Covering the best of both worlds
Christian Heilmann
 
Non-trivial pursuits: Learning machines and forgetful humans
Non-trivial pursuits: Learning machines and forgetful humansNon-trivial pursuits: Learning machines and forgetful humans
Non-trivial pursuits: Learning machines and forgetful humans
Christian Heilmann
 
Progressive Web Apps - Bringing the web front and center
Progressive Web Apps - Bringing the web front and center Progressive Web Apps - Bringing the web front and center
Progressive Web Apps - Bringing the web front and center
Christian Heilmann
 
CSS vs. JavaScript - Trust vs. Control
CSS vs. JavaScript - Trust vs. ControlCSS vs. JavaScript - Trust vs. Control
CSS vs. JavaScript - Trust vs. Control
Christian Heilmann
 
Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017
Christian Heilmann
 
The Soul in The Machine - Developing for Humans (FrankenJS edition)
The Soul in The Machine - Developing for Humans (FrankenJS edition)The Soul in The Machine - Developing for Humans (FrankenJS edition)
The Soul in The Machine - Developing for Humans (FrankenJS edition)
Christian Heilmann
 

More from Christian Heilmann (20)

Develop, Debug, Learn? - Dotjs2019
Develop, Debug, Learn? - Dotjs2019Develop, Debug, Learn? - Dotjs2019
Develop, Debug, Learn? - Dotjs2019
 
Hinting at a better web
Hinting at a better webHinting at a better web
Hinting at a better web
 
Taking the "vile" out of privilege
Taking the "vile" out of privilegeTaking the "vile" out of privilege
Taking the "vile" out of privilege
 
Seven ways to be a happier JavaScript developer - NDC Oslo
Seven ways to be a happier JavaScript developer - NDC OsloSeven ways to be a happier JavaScript developer - NDC Oslo
Seven ways to be a happier JavaScript developer - NDC Oslo
 
Artificial intelligence for humans… #AIDC2018 keynote
Artificial intelligence for humans… #AIDC2018 keynoteArtificial intelligence for humans… #AIDC2018 keynote
Artificial intelligence for humans… #AIDC2018 keynote
 
Killing the golden calf of coding - We are Developers keynote
Killing the golden calf of coding - We are Developers keynoteKilling the golden calf of coding - We are Developers keynote
Killing the golden calf of coding - We are Developers keynote
 
Progressive Web Apps - Techdays Finland
Progressive Web Apps - Techdays FinlandProgressive Web Apps - Techdays Finland
Progressive Web Apps - Techdays Finland
 
Taking the "vile" out of privilege
Taking the "vile" out of privilegeTaking the "vile" out of privilege
Taking the "vile" out of privilege
 
Five ways to be a happier JavaScript developer
Five ways to be a happier JavaScript developerFive ways to be a happier JavaScript developer
Five ways to be a happier JavaScript developer
 
Taking the P out of PWA
Taking the P out of PWATaking the P out of PWA
Taking the P out of PWA
 
Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"
 
You learned JavaScript - now what?
You learned JavaScript - now what?You learned JavaScript - now what?
You learned JavaScript - now what?
 
Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"
 
Progressive Web Apps - Covering the best of both worlds - DevReach
Progressive Web Apps - Covering the best of both worlds - DevReachProgressive Web Apps - Covering the best of both worlds - DevReach
Progressive Web Apps - Covering the best of both worlds - DevReach
 
Progressive Web Apps - Covering the best of both worlds
Progressive Web Apps - Covering the best of both worldsProgressive Web Apps - Covering the best of both worlds
Progressive Web Apps - Covering the best of both worlds
 
Non-trivial pursuits: Learning machines and forgetful humans
Non-trivial pursuits: Learning machines and forgetful humansNon-trivial pursuits: Learning machines and forgetful humans
Non-trivial pursuits: Learning machines and forgetful humans
 
Progressive Web Apps - Bringing the web front and center
Progressive Web Apps - Bringing the web front and center Progressive Web Apps - Bringing the web front and center
Progressive Web Apps - Bringing the web front and center
 
CSS vs. JavaScript - Trust vs. Control
CSS vs. JavaScript - Trust vs. ControlCSS vs. JavaScript - Trust vs. Control
CSS vs. JavaScript - Trust vs. Control
 
Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017
 
The Soul in The Machine - Developing for Humans (FrankenJS edition)
The Soul in The Machine - Developing for Humans (FrankenJS edition)The Soul in The Machine - Developing for Humans (FrankenJS edition)
The Soul in The Machine - Developing for Humans (FrankenJS edition)
 

Recently uploaded

20240702 QFM021 Machine Intelligence Reading List June 2024
20240702 QFM021 Machine Intelligence Reading List June 202420240702 QFM021 Machine Intelligence Reading List June 2024
20240702 QFM021 Machine Intelligence Reading List June 2024
Matthew Sinclair
 
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-InTrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc
 
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Bert Blevins
 
Best Programming Language for Civil Engineers
Best Programming Language for Civil EngineersBest Programming Language for Civil Engineers
Best Programming Language for Civil Engineers
Awais Yaseen
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
RaminGhanbari2
 
Password Rotation in 2024 is still Relevant
Password Rotation in 2024 is still RelevantPassword Rotation in 2024 is still Relevant
Password Rotation in 2024 is still Relevant
Bert Blevins
 
UiPath Community Day Kraków: Devs4Devs Conference
UiPath Community Day Kraków: Devs4Devs ConferenceUiPath Community Day Kraków: Devs4Devs Conference
UiPath Community Day Kraków: Devs4Devs Conference
UiPathCommunity
 
DealBook of Ukraine: 2024 edition
DealBook of Ukraine: 2024 editionDealBook of Ukraine: 2024 edition
DealBook of Ukraine: 2024 edition
Yevgen Sysoyev
 
What's New in Copilot for Microsoft365 May 2024.pptx
What's New in Copilot for Microsoft365 May 2024.pptxWhat's New in Copilot for Microsoft365 May 2024.pptx
What's New in Copilot for Microsoft365 May 2024.pptx
Stephanie Beckett
 
Comparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdfComparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdf
Andrey Yasko
 
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdfPigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions
 
The Rise of Supernetwork Data Intensive Computing
The Rise of Supernetwork Data Intensive ComputingThe Rise of Supernetwork Data Intensive Computing
The Rise of Supernetwork Data Intensive Computing
Larry Smarr
 
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
Toru Tamaki
 
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Chris Swan
 
Calgary MuleSoft Meetup APM and IDP .pptx
Calgary MuleSoft Meetup APM and IDP .pptxCalgary MuleSoft Meetup APM and IDP .pptx
Calgary MuleSoft Meetup APM and IDP .pptx
ishalveerrandhawa1
 
7 Most Powerful Solar Storms in the History of Earth.pdf
7 Most Powerful Solar Storms in the History of Earth.pdf7 Most Powerful Solar Storms in the History of Earth.pdf
7 Most Powerful Solar Storms in the History of Earth.pdf
Enterprise Wired
 
Observability For You and Me with OpenTelemetry
Observability For You and Me with OpenTelemetryObservability For You and Me with OpenTelemetry
Observability For You and Me with OpenTelemetry
Eric D. Schabell
 
How to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptxHow to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptx
Adam Dunkels
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
HackersList
 
WPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide DeckWPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide Deck
Lidia A.
 

Recently uploaded (20)

20240702 QFM021 Machine Intelligence Reading List June 2024
20240702 QFM021 Machine Intelligence Reading List June 202420240702 QFM021 Machine Intelligence Reading List June 2024
20240702 QFM021 Machine Intelligence Reading List June 2024
 
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-InTrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
 
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
 
Best Programming Language for Civil Engineers
Best Programming Language for Civil EngineersBest Programming Language for Civil Engineers
Best Programming Language for Civil Engineers
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
 
Password Rotation in 2024 is still Relevant
Password Rotation in 2024 is still RelevantPassword Rotation in 2024 is still Relevant
Password Rotation in 2024 is still Relevant
 
UiPath Community Day Kraków: Devs4Devs Conference
UiPath Community Day Kraków: Devs4Devs ConferenceUiPath Community Day Kraków: Devs4Devs Conference
UiPath Community Day Kraków: Devs4Devs Conference
 
DealBook of Ukraine: 2024 edition
DealBook of Ukraine: 2024 editionDealBook of Ukraine: 2024 edition
DealBook of Ukraine: 2024 edition
 
What's New in Copilot for Microsoft365 May 2024.pptx
What's New in Copilot for Microsoft365 May 2024.pptxWhat's New in Copilot for Microsoft365 May 2024.pptx
What's New in Copilot for Microsoft365 May 2024.pptx
 
Comparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdfComparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdf
 
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdfPigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdf
 
The Rise of Supernetwork Data Intensive Computing
The Rise of Supernetwork Data Intensive ComputingThe Rise of Supernetwork Data Intensive Computing
The Rise of Supernetwork Data Intensive Computing
 
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
 
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
Fluttercon 2024: Showing that you care about security - OpenSSF Scorecards fo...
 
Calgary MuleSoft Meetup APM and IDP .pptx
Calgary MuleSoft Meetup APM and IDP .pptxCalgary MuleSoft Meetup APM and IDP .pptx
Calgary MuleSoft Meetup APM and IDP .pptx
 
7 Most Powerful Solar Storms in the History of Earth.pdf
7 Most Powerful Solar Storms in the History of Earth.pdf7 Most Powerful Solar Storms in the History of Earth.pdf
7 Most Powerful Solar Storms in the History of Earth.pdf
 
Observability For You and Me with OpenTelemetry
Observability For You and Me with OpenTelemetryObservability For You and Me with OpenTelemetry
Observability For You and Me with OpenTelemetry
 
How to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptxHow to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptx
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
 
WPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide DeckWPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide Deck
 

Progressive Enhancement with JavaScript and Ajax

  • 1. Progressive Enhancement With JavaScript and Ajax Highland Fling 2007 Christian Heilmann
  • 2. • As web developers we are in a very lucky position. • Unlike real world products, web sites do not have a fixed state. • Web documents were right from the start meant to be adaptive to their environment. • By keeping things flexible and adaptive we can please all consumers. http://www.flickr.com/photos/oskay/420878465/
  • 3. • Real life enhancement
  • 4. • Real life has some ideas of progressive enhancement, most of which are old chestnuts: – Measure twice, cut once. – Check the depth of the lake before you plunge in. – Try before you buy – = Use Bittorrent
  • 5. • The same logic applies to JavaScript and Ajax: – Test if JavaScript is available. – Test if the DOM is supported – Test for the HTML you want to use. – Test for any object you want to use. – Don’t rely on the connection to be available.
  • 6. • Testing for JavaScript is easy, simply don’t make your product rely on JavaScript: – Death to href=“javascript:foo()” – Death to onclick=“foo()” – Who the hell ever told you to use onclick=“javascript:foo()” ? • Don’t mix HTML and JavaScript, use the DOM to access HTML. • The DOM is your API to the document. http://www.flickr.com/photos/jungle_boy/140233674/
  • 7. • Testing for DOM support is as easy: – Check that getElementById and is available. – If you still care about Opera 6 also test for createTextNode function init(){ if( document.getElementById && document.createTextNode ) // we have DOM, wahey! checkDependencies(); } }
  • 8. • Testing for the HTML you need is also easy: – Check that all the elements are available. function checkDependencies(){ var usedIds = ['nav','content']; var isOk = true; for(var i = 0; usedIds[i]!==undefined; i++) { if( !document.getElementById( usedIds[i] ) ){ isOk = false; } } return isOk; }
  • 9. • Testing for the HTML you need is also easy: – Check that all the elements are available. – Check that they also have the right child nodes function myNavStuff(){ var n = document.getElementById('nav'); if(n.getElementsByTagName('ul').length > 0){ // I got your code right here! } }
  • 10. • Testing for object availability – Simply test for any object before you access it var request; try{ request = new XMLHttpRequest(); }catch(error){ try{ request = new ActiveXObject(quot;Microsoft.XMLHTTPquot;); }catch(error){ return true; } }
  • 11. • Prepare for unreliable and flaky connections. – Set a timer to time out and abort the connection after a certain period has passed. – Define a success and failure case for any connection. – Make sure you avoid caching issues by setting an expired HTTP header on your Ajax calls
  • 12. • This is the “how” of progressive enhancement with JavaScript and Ajax. • The question however is why use JavaScript at all? • After all it can be turned off.
  • 13. • Instead we can use the amazing new trick called “CSS only solutions”. • Why bother with writing complex scripts when you can do the same with some easy lines of CSS? • Showing and hiding menus? #nav li ul { display:none } #nav li:hover ul { display:block }
  • 14. • Using CSS for behaviour is based on one trick, which is pseudo selectors like :hover, :active and :focus • These have several problems: – Some browsers only support them on interactive elements like links and buttons. – They have a strict on or off state. – and they fail for a certain user group…
  • 15. • Keyboard access support in HTML and browsers is bad. • Support for :active and :focus is even worse. • Which means that CSS only solutions with keyboard support turn out to be HTML soup nightmares (nesting tables inside links…)
  • 17. • In essence, CSS only solutions are a bit of a shifty character and you don’t know if you can trust them. • It is also debateable if CSS only solutions are in the end easier than JS solutions.
  • 18. • JavaScript has so many more options for you: – Real testing for support of objects – Real event handling including simulation of application keyboard handling (cursors instead of tabbing) – timeout() to allow for user errors – Creation of necessary markup on demand.
  • 19. • JavaScript has so many more options for you: – Test of available screen estate – Loading of extra content on demand http://www.flickr.com/photos/drewm/227562632/
  • 21. • Does this mean that only scripters should create interfaces and CSS is not needed? • Not at all, as first of all CSS can help JavaScript a lot. http://www.flickr.com/photos/drewm/225911999/in/set-507727/
  • 22. • Task: Hide all list items in the first UL – A lot of code, that one… window.onload = function(){ if(document.getElementById && document.createTextNode){ var uls = document.getElementsByTagName('ul'); if(uls.length > 0){ var lis = uls[0].getElementsByTagName('li'); for(var i = 0, j = lis.length; i < j; i++){ lis[i].style.display = 'none'; } } } }
  • 23. • Task: Hide all list items in the first UL – How about leaving the main job to the CSS parser? window.onload = function(){ if(document.getElementById && document.createTextNode){ var b = document.body; b.className += b.className ? ' dynamic' : 'dynamic'; } } <style type=quot;text/cssquot;> body.dynamic ul li{ display:none; } </style>
  • 24. • This also allows you to keep the whole look and feel of your scripts in the hands of the CSS maintainers. • All you need to do is make sure you communicate the class and ID names with them.
  • 25. • You achieve highest flexibility by separating those and the text labels out into own includes: slideshow.js slideshow-css.js slideshow-labels.js slideshow.css = { 'navigationID':'nav', 'contentID':'main', 'hide class':'hide' }
  • 26. • There is a danger in progressive enhancement with JavaScript though: • Be aware that what you enhance is as important as how you enhance it. • A solution that doesn’t really make sense shouldn’t be enhanced, but changed.
  • 27. • Useless progressive enhancement. Pointless name dropping Get on with it…
  • 28. • It is important to know your audience • It is important to keep up with technology as a lot of truths of the past are not applicable any longer.
  • 29. • Research what can be enhanced and how. • Example “connected select boxes”:
  • 30. • Research what can be enhanced and how. • Example “hcard to Googlemap”
  • 31. • So why isn’t progressive enhancement yet a common practice? http://www.flickr.com/photos/urlgirl/199044322/
  • 33. • Stockholm Syndrome • Enhanced interfaces don’t go down too well with the mainstream audience, we are used to waiting and things breaking. • Technologies are perceived as given, no need to improve them.
  • 34. • Monetary reasons • Progressive Enhancement costs money • As with any best practice there is no immediate tangible benefit
  • 35. • Show desire • You can help to change this • Look at things, and see how you can improve them • If need be, do it on the sly (hcard in Yahoo! Local)
  • 36. • Benefit from experience of others • A lot of tricks that helped us in the past can be brushed up and renewed now. • Example “lowsrc”
  • 37. • Keep your feelers out there. • Read up on technologies and what is done with them. • Hot: – http://www.w3.org/TR/aria- roadmap/ – http://www.whatwg.org/
  • 38. • Contact, subscribe, talk, share. • If you know something, please tell others: http://makemeaspeaker.com
  • 39. • Good progressive enhancement helps the user, and not the developer. – Automatic saving of drafts in email apps – Parallel upload of content while you can add meta data – Making it easy and fun to add content. – Preventing unnecessary steps. http://www.flickr.com/photos/radiofree/286343390/