SlideShare a Scribd company logo
Building with
    JavaScript
write less by using the right tools


                                           Christian Heilmann
                   Framsia Meetup, Oslo, Norway, October 2010
Javascript has won the
title of most used web
programming language.
The reason is that its
syntax is forgiving and
that it comes with a few
incredibly powerful
tools.
The other reason is that
libraries allow us to
write JavaScript instead
of catering to browsers
and fixing them.
This comes with a few
dangers though.
The “write less, achieve
more” attitude is a
scam.
Less code does not
mean a better solution.
Less redundant code
means a good solution.
If writing a few more
lines of code makes it
easier for others to use
what you have done
then you built a
professional product.
If you are the only one
understanding what is
going on you used the
web as a dumping
ground.
You can write incredibly
small solutions that
work for everyone.
If you use the right
technology for the job.
If you want to build for
people, use progressive
enhancement.
Progressive
enhancement means
not making everything
work in IE6.
It means checking the
depth of the river before
diving in.
The physical world is in
a final state.
A pair of scissors
cannot change
themselves when a left-
handed person picks
them up instead of a
right-handed person.
Building with JavaScript -  write less by using the right tools
Web applications and
systems can do that - if
we allow them to.
A lot of the criticism of
progressive
enhancement is based
on people working in
walled-off
environments.
If all you write is an
iPad app or something
for the iPhone you have
it much easier than
building a web app.
A Chrome extension will
never have to work in
IE6 - there is no point in
that.
On the other hand an
Opera extension is a
W3C widget and also
works on Vodafone
mobile phones.
And all 20 users will be
very excited about that.
Anyways, let’s go
through a few examples
and how to write a
damn small amount of
JS to achieve a lot.
http://10k.aneventapart.com/Entry/185
http://github.com/codepo8/worldinfo
This is a system where I
was only allowed to use
JavaScript.
Were this to be a
product, I’d have taken
another approach -
more later.
So here’s what I did...
Get all the geographical
information of all the
countries in the world
using the Yahoo
GeoPlanet API.
Building with JavaScript -  write less by using the right tools
Store the information in
an object and loop
through it to create the
navigation.
Building with JavaScript -  write less by using the right tools
I am using buttons for
the navigation
elements. Why?
There is no real
navigation happening
here. Everything is
dependent on JS. This is
what buttons are for.
And they are keyboard
accessible.
This should be a huge
nested list. Why isn’t it
visible?
Use CSS instead of
simulating it.
Hide the loading
message, add the new
HTML and show it
again.
Then show the first
entry and make the
buttons trigger the right
functionality.
Instead of storing data
in DOM elements with
custom attributes I have
two variables to store
the currently shown
section and the full data
set.
Building with JavaScript -  write less by using the right tools
Instead of looping a lot
of elements and
assigning redundant
event handlers one
event does the whole
job.
The differentiator is the
length of the button
text. If it is one
character show the
section - otherwise load
the country info.
Building with JavaScript -  write less by using the right tools
Next step was to show
the country info.
Fade the old container
and show a loading
message.
Read the country name
from the button and get
the value which is the
number of the data set.
Building with JavaScript -  write less by using the right tools
Building with JavaScript -  write less by using the right tools
That did it - a bit more
code than “real” jQuery
developers would have
done...
...but it is bullet proof
and does the least
amount of DOM lookups
possible.
However, it was slow as
heck as it takes time to
download the world.
As you’ve seen earlier, it
loads much faster the
second time you go to
the page.
The trick is that I am
using “HTML5 Storage”
to cache the whole
navigation.
Building with JavaScript -  write less by using the right tools
That way the interface
is loaded from HD and
not from the web.
You can extend that to
cache full HTML
interfaces.
http://www.wait-till-i.com/2010/08/26/using-html5-storage-to-
                cache-application-interfaces/
Other progressive
enhancement
approaches use a single
page as the data
container.
http://isithackday.com/demos/warwickshire/
http://github.com/codepo8/warwickshire
http://www.youtube.com/watch?v=_uAOyzw50PY
My favourite approach
uses the backend as a
simple API.
http://www.youtube.com/watch?v=i_1sVnNkN2M
http://isithackday.com/hacks/flickrcollector/
http://github.com/codepo8/flickrcollector
Write the app as a
simple form submit in
PHP...
Building with JavaScript -  write less by using the right tools
Then return chunks of
HTML according to the
parameters that came
in.
Building with JavaScript -  write less by using the right tools
Building with JavaScript -  write less by using the right tools
All you need to do in JS
then is to override the
requests with Ajax calls.
Building with JavaScript -  write less by using the right tools
You maintain your
whole app on the server
and can monitor and
cache like heck.
And you still use the
goodness that is
JavaScript on top of
that.
The dangers of “useful”
shortcuts.
Getting JSON-P data in
jQuery:
$.ajax({
  url: url,
  dataType: 'jsonp',
  jsonp: 'callback',
  jsonpCallback: 'datain'
});
function datain(data){
}
Getting JSON-P data in
jQuery (shorter):

$.getJSON(url+'&callback=?',
 function(data){
});
$.ajax():
http://{...}&
format=json&callback=datain


$.getJSON():
http://{...}&
format=json&callback=jsonp1282497813335


                     random number
getJSON() breaks the
cache of the service you
request.
As each request has a
unique URL - even when
you ask for the same
data...
... you’ll end up being
banned for an hour very
quickly.
less code == better?
Analysing the impact
should be part of your
solution.
The danger of quick
solutions is that people
tend to copy and paste
them and not try to
integrate them.
And if we put together
solutions from lots of
copy and paste
examples they become
huge and hard to
understand.
Which is probably the
main reason of all the
massive CSS files out
there.
Use technologies to
their strengths to write
very short and efficient
code.
★   Progressive Enhancement with
    the correct HTML
★   Event Delegation
★   Adding CSS classes instead of
    using hide()
★   Rendering on the server side.
★   Caching on the client side.
★   Configuration objects and
    variable caches instead of
    custom attributes.
Keep an eye out on how
libraries help you with
writing bullet proof code
- not how to write small
scripts.
Christian Heilmann
http://wait-till-i.com        Thanks!
http://developer-evangelism.com
http://twitter.com/codepo8

More Related Content

What's hot (7)

PPT
LatJUG. Google App Engine
denis Udod
 
PDF
Analyse Yourself
Norberto Leite
 
PPTX
Before start
Medhat Dawoud
 
PDF
Web Performance in the Age of HTTP2 - Topconf Tallinn 2016 - Holger Bartel
Holger Bartel
 
PPSX
Putting SOAP to REST
Igor Moochnick
 
PDF
Websocket in iOS application to create real-time applications
Roman Barzyczak
 
PPT
Dynamic website
Ryan Scheel
 
LatJUG. Google App Engine
denis Udod
 
Analyse Yourself
Norberto Leite
 
Before start
Medhat Dawoud
 
Web Performance in the Age of HTTP2 - Topconf Tallinn 2016 - Holger Bartel
Holger Bartel
 
Putting SOAP to REST
Igor Moochnick
 
Websocket in iOS application to create real-time applications
Roman Barzyczak
 
Dynamic website
Ryan Scheel
 

Similar to Building with JavaScript - write less by using the right tools (20)

PPTX
Advanced JavaScript
Mahmoud Tolba
 
KEY
10 Years of JavaScript
Mike de Boer
 
PDF
Progressive Enhancement with JavaScript and Ajax
Christian Heilmann
 
PPTX
CT presentatie JQuery 7.12.11
virtualsciences41
 
PDF
Jazz up your JavaScript: Unobtrusive scripting with JavaScript libraries
Simon Willison
 
KEY
Javascript done right - Open Web Camp III
Dirk Ginader
 
PPT
jQuery with javascript training by Technnovation Labs
Prasad Shende
 
PDF
Maintainable Javascript carsonified
Christian Heilmann
 
PDF
What's this jQuery? Where it came from, and how it will drive innovation
Marakana Inc.
 
PPT
CTS Conference Web 2.0 Tutorial Part 2
Geoffrey Fox
 
PPT
JavaScript Misunderstood
Bhavya Siddappa
 
PPTX
Javascript best practices
Jayanga V. Liyanage
 
KEY
User Interface Development with jQuery
colinbdclark
 
PPTX
Workshop Intro: FrontEnd General Overview
Visual Engineering
 
KEY
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
PDF
Building a JavaScript Library
jeresig
 
PDF
FFWD.PRO - It's not you, It's me (or how to avoid being coupled with a Javasc...
Marco Cedaro
 
PDF
Performance Optimization and JavaScript Best Practices
Doris Chen
 
PPTX
Java scriptforjavadev part2a
Makarand Bhatambarekar
 
PDF
JavascriptMVC: Another choice of web framework
Alive Kuo
 
Advanced JavaScript
Mahmoud Tolba
 
10 Years of JavaScript
Mike de Boer
 
Progressive Enhancement with JavaScript and Ajax
Christian Heilmann
 
CT presentatie JQuery 7.12.11
virtualsciences41
 
Jazz up your JavaScript: Unobtrusive scripting with JavaScript libraries
Simon Willison
 
Javascript done right - Open Web Camp III
Dirk Ginader
 
jQuery with javascript training by Technnovation Labs
Prasad Shende
 
Maintainable Javascript carsonified
Christian Heilmann
 
What's this jQuery? Where it came from, and how it will drive innovation
Marakana Inc.
 
CTS Conference Web 2.0 Tutorial Part 2
Geoffrey Fox
 
JavaScript Misunderstood
Bhavya Siddappa
 
Javascript best practices
Jayanga V. Liyanage
 
User Interface Development with jQuery
colinbdclark
 
Workshop Intro: FrontEnd General Overview
Visual Engineering
 
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
Building a JavaScript Library
jeresig
 
FFWD.PRO - It's not you, It's me (or how to avoid being coupled with a Javasc...
Marco Cedaro
 
Performance Optimization and JavaScript Best Practices
Doris Chen
 
Java scriptforjavadev part2a
Makarand Bhatambarekar
 
JavascriptMVC: Another choice of web framework
Alive Kuo
 
Ad

More from Christian Heilmann (20)

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

Recently uploaded (20)

PPTX
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Difference between write and update in odoo 18
Celine George
 
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
epi editorial commitee meeting presentation
MIPLM
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Horarios de distribución de agua en julio
pegazohn1978
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Controller Request and Response in Odoo18
Celine George
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Difference between write and update in odoo 18
Celine George
 

Building with JavaScript - write less by using the right tools