Unobtrusive javascript
 JavaScript guru Douglas Crockford calls the
browser “the most hostile programming
environment in the world.”
 He said this before mobile browsing was even a
thing at which point he said:
 “I used to say that the web browser was the
most hostile programming environment ever
devised, but that was before I found out about
Mobile.”
 Infinite combinations of hardware and software
The front end stack
HTML
 HTML is forgiving and browsers can deal with
invalid markup
 Markup that doesn't meet the spec will still give
you a mostly working page
CSS
 CSS is aloof. If it encounters something it
doesn't understand, it just shrugs and moves on
 Some IE CSS hacks take advantage of CSS
quirks (It's not a bug, it's a feature)
Javascript
 Javascript is a quitter. If it encounters a problem it just
gives up and goes on strike.
 Some browsers may ignore your script completely
 Even when a browser can support JavaScript, users may
turn it off for security reasons, or their corporate firewall
may block it
 Even when a browser supports JavaScript, it may not
understand parts of your script because it has a
proprietary implementation of some parts of the DOM
specification (usually IE)
 Even when the script is interpreted correctly, it may
depend on HTML that is very complicated and/or might
change in unpredictable ways.
A lesson from history
First world problems
 Last February the download button for google
chrome broke.
 Nobody could download chrome for 12 hours
 It was caused by a javascript error
 This was an easy problem to avoid
What went wrong?
 Somebody broke an unrelated javascript library
used by the google chrome website and
included in all pages
 Uncaught TypeError: Cannot read property
'value' of undefined
 Because google did not include a href, the
browser could do nothing except sit there
looking embarrassed
No href
What google did wrong
 Didn't follow the rules of unobtrusive javascript
 Created a single point of failure
 Ignored web standards
 Trusted core functionality to the most fragile
part of the front end stack
Why do you hate javascript?
 Javascript is great
 But don't start with it
 It's like building a house by choosing the
wallpaper first
 The purpose of JavaScript is to add a layer of
usability to your site. If the script is the entire
usability layer (in other words, if the site is
unusable without JavaScript), your javascript is
not unobtrusive.
Principles of unobtrusive javascript
 Your site should work without JavaScript.
 If JavaScript happens to be enabled, you can
present your users with an extra layer of
usability; a layer that allows them to perform
their tasks more quickly, and that avoids jarring
page reloads where possible.
 JavaScript is unsafe. That is, you should
*never* trust in JavaScript-only routines for
crucial tasks such as checking user input in a
form.
p.s. Google still don't have a href on that link.
Maybe they invested in better functional tests
instead?

Unobtrusive javascript

  • 1.
  • 2.
     JavaScript guruDouglas Crockford calls the browser “the most hostile programming environment in the world.”  He said this before mobile browsing was even a thing at which point he said:  “I used to say that the web browser was the most hostile programming environment ever devised, but that was before I found out about Mobile.”  Infinite combinations of hardware and software The front end stack
  • 3.
    HTML  HTML isforgiving and browsers can deal with invalid markup  Markup that doesn't meet the spec will still give you a mostly working page
  • 4.
    CSS  CSS isaloof. If it encounters something it doesn't understand, it just shrugs and moves on  Some IE CSS hacks take advantage of CSS quirks (It's not a bug, it's a feature)
  • 5.
    Javascript  Javascript isa quitter. If it encounters a problem it just gives up and goes on strike.  Some browsers may ignore your script completely  Even when a browser can support JavaScript, users may turn it off for security reasons, or their corporate firewall may block it  Even when a browser supports JavaScript, it may not understand parts of your script because it has a proprietary implementation of some parts of the DOM specification (usually IE)  Even when the script is interpreted correctly, it may depend on HTML that is very complicated and/or might change in unpredictable ways.
  • 6.
  • 7.
    First world problems Last February the download button for google chrome broke.  Nobody could download chrome for 12 hours  It was caused by a javascript error  This was an easy problem to avoid
  • 8.
    What went wrong? Somebody broke an unrelated javascript library used by the google chrome website and included in all pages  Uncaught TypeError: Cannot read property 'value' of undefined  Because google did not include a href, the browser could do nothing except sit there looking embarrassed
  • 9.
  • 10.
    What google didwrong  Didn't follow the rules of unobtrusive javascript  Created a single point of failure  Ignored web standards  Trusted core functionality to the most fragile part of the front end stack
  • 11.
    Why do youhate javascript?  Javascript is great  But don't start with it  It's like building a house by choosing the wallpaper first  The purpose of JavaScript is to add a layer of usability to your site. If the script is the entire usability layer (in other words, if the site is unusable without JavaScript), your javascript is not unobtrusive.
  • 12.
    Principles of unobtrusivejavascript  Your site should work without JavaScript.  If JavaScript happens to be enabled, you can present your users with an extra layer of usability; a layer that allows them to perform their tasks more quickly, and that avoids jarring page reloads where possible.  JavaScript is unsafe. That is, you should *never* trust in JavaScript-only routines for crucial tasks such as checking user input in a form.
  • 13.
    p.s. Google stilldon't have a href on that link. Maybe they invested in better functional tests instead?