SlideShare una empresa de Scribd logo
1 de 95
Descargar para leer sin conexión
THE MYSTERIES OF
  JAVASCRIPT-FU
 Dan Webb (dan@danwebb.net)
“Ah, JavaScript, the language
     we all love to hate”
        Geoffrey Grosenbach, Peepcode
A peasant’s language
Web 2.0 has forced
‘real’ programmers to
        master it
JavaScript-Fu is not easy
       to master
The web is awash with
 bad examples and
  worse resources
Developers forced into
refuge behind libraries
    and frameworks
THE ANCIENT MANUALS
      OF JAVASCRIPT-FU
    The Tao Of The Event Handler!
❖

    5 Methods Of DOM Fist!
❖

    Lighting Script Style!
❖

    Iron Ajax Technique!
❖
TRANSLATED...

    Working with events
❖

    Working with DOM elements
❖

    Optimisation
❖

    Progressive enhancement
❖
THE TAO OF THE
EVENT HANDLER
The essence of browser
 scripting is defining
       behavior
Big differences in both
browser implementation
      and opinion
The main battle has
always been in one
      area...
INLINE VS SCRIPTED
INLINE EVENT HANDLERS
INLINE EVENT HANDLERS




Applied as soon as the browser
loads the HTML
INLINE EVENT HANDLERS




But what happens when there
is more than one....
Bad JavaScript-fu
SCRIPT-BASED
       EVENT HANDLERS
Attached after element
      has loaded
Very DRY
Separate JavaScript out
in a similar way to CSS
Large numbers of event
handlers choke browsers
WHICH WAY?
    Use script-based event handling by
❖
    default
    If the page is large and this method
❖
    results in unresponsiveness try event
    delegation
    If all else fails go for inline event
❖
    handling
EVENT BUBBLING

 <body>     handler


 <div>

 <p>

 <a>         click
EVENT DELEGATION
BETTER INLINE HANDLERS
BETTER INLINE HANDLERS
5 METHODS OF
  DOM FIST
There are 5 methods for
    updating HTML
3 official methods
       (W3C)
$('kungfu').appendChild(node);
$('kungfu').insertBefore(node, child);
$('kungfu').replaceChild(node, child);
1 non-standard method
(you guessed it, from IE)
DOM VS innerHTML
DOM METHODS
Insert elements with
      precision
But you need to create
   the nodes first...
ARGH!
LOWPRO’S DOM BUILDER
INNERHTML
Can shift large amount
  of HTML quickly...
...but you don't get
    much control
Incredibly simple
 to use with Ajax
WHICH WAY?

    No clear winner
❖

    DOM is good for more surgical
❖
    manipulation
    innerHTML is good for replacing large
❖
    amounts of content or simple jobs
and the final method...
THE BASTARD SON
LIGHTENING
SCRIPT STYLE
~134KB
5 HTTP
requests
Takes time to download
  and evaluate script
The less JavaScript
    the better
Browser normally only
try to load 2 resources
      concurrently
Combine .js files
Use GZIP compression
not JS based minification
Make sure everything
    is cachable
FASTER LOOPS
FASTER LOOPS
BE CAREFUL WITH
            SELECTORS
IRON AJAX
TECHNIQUE
RULE #1:
BROWSERS
  SUCK
Main browsers are
getting better quickly
But what about the
     others?
Corporate
security
and
firewalls
often block
JavaScript
The traditional answer
      from Rails:
But why turn users away
  if you don’t have to?
PROGRESSIVE
        ENHANCEMENT
1. Start with a working plain HTML app
2. Test if necessary browser features are
   there (XMLHttpRequest, canvas etc)
3. If present then apply extra JS powered
   features to the UI
It's easy to apply
    this to Ajax
IRON AJAX
                                  action.rhtml

              respond_to :html

       POST


                Controller       render :partial




                                   _partial.rhtml
IRON AJAX
                                      action.rhtml

                  respond_to :html

        POST


                    Controller       render :partial


      Ajax POST

                   respond_to :js


                                       _partial.rhtml
IRON AJAX
                                      action.rhtml

                  respond_to :html

        POST


                    Controller       render :partial


      Ajax POST

                   respond_to :js



                                         action.rjs
THE HTML
THE HTML
THE JAVASCRIPT
THE CONTROLLER
THE CONTROLLER
Easy
I know what you’re
     thinking
But it won’t work if...
Try progressive
enhancement first
LEARNING MORE


    Dan Webb: http://www.danwebb.net
❖

    Low Pro: http://www.danwebb.net/lowpro
❖
LEARNING MORE
QUESTIONS?

Más contenido relacionado

La actualidad más candente

Gearman: A Job Server made for Scale
Gearman: A Job Server made for ScaleGearman: A Job Server made for Scale
Gearman: A Job Server made for Scale
Mike Willbanks
 
Andrew Mykhaliuk - Sorry, I need to make a build for frontend
Andrew Mykhaliuk - Sorry, I need to make a build for frontendAndrew Mykhaliuk - Sorry, I need to make a build for frontend
Andrew Mykhaliuk - Sorry, I need to make a build for frontend
OdessaJS Conf
 

La actualidad más candente (20)

Managing Modules Without Going Crazy (NPW 2007)
Managing Modules Without Going Crazy (NPW 2007)Managing Modules Without Going Crazy (NPW 2007)
Managing Modules Without Going Crazy (NPW 2007)
 
Gearman: A Job Server made for Scale
Gearman: A Job Server made for ScaleGearman: A Job Server made for Scale
Gearman: A Job Server made for Scale
 
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015
 
WebNano - Ideas for Web Frameworks
WebNano - Ideas for Web FrameworksWebNano - Ideas for Web Frameworks
WebNano - Ideas for Web Frameworks
 
Increase automation to rest
Increase automation to restIncrease automation to rest
Increase automation to rest
 
PHP Aberdeen Quick optimisation of PHP with Webgrind
PHP Aberdeen Quick optimisation of PHP with WebgrindPHP Aberdeen Quick optimisation of PHP with Webgrind
PHP Aberdeen Quick optimisation of PHP with Webgrind
 
5 Quick JavaScript Performance Improvement Tips
5 Quick JavaScript Performance Improvement Tips5 Quick JavaScript Performance Improvement Tips
5 Quick JavaScript Performance Improvement Tips
 
Rubyhosting
RubyhostingRubyhosting
Rubyhosting
 
Prerendering with Nokogiri
Prerendering with NokogiriPrerendering with Nokogiri
Prerendering with Nokogiri
 
SQL Server - CLR integration
SQL Server - CLR integrationSQL Server - CLR integration
SQL Server - CLR integration
 
Andrew Mykhaliuk - Sorry, I need to make a build for frontend
Andrew Mykhaliuk - Sorry, I need to make a build for frontendAndrew Mykhaliuk - Sorry, I need to make a build for frontend
Andrew Mykhaliuk - Sorry, I need to make a build for frontend
 
I Promise You
I Promise YouI Promise You
I Promise You
 
High performance java script why everything youve been taught is wrong
High performance java script why everything youve been taught is wrongHigh performance java script why everything youve been taught is wrong
High performance java script why everything youve been taught is wrong
 
CoffeeScript the Awesome
CoffeeScript the AwesomeCoffeeScript the Awesome
CoffeeScript the Awesome
 
Asynchronous Personalization at Groupon - JSConf 2011
Asynchronous Personalization at Groupon - JSConf 2011Asynchronous Personalization at Groupon - JSConf 2011
Asynchronous Personalization at Groupon - JSConf 2011
 
DevOps For Small Teams
DevOps For Small TeamsDevOps For Small Teams
DevOps For Small Teams
 
Managing Complexity with Module::Release
Managing Complexity with Module::ReleaseManaging Complexity with Module::Release
Managing Complexity with Module::Release
 
Gearman
GearmanGearman
Gearman
 
Sharing the pain using Protractor
Sharing the pain using ProtractorSharing the pain using Protractor
Sharing the pain using Protractor
 
Coffee script throwdown
Coffee script throwdownCoffee script throwdown
Coffee script throwdown
 

Destacado

YUI 3: The Most Advance JavaScript Library in the World
YUI 3: The Most Advance JavaScript Library in the WorldYUI 3: The Most Advance JavaScript Library in the World
YUI 3: The Most Advance JavaScript Library in the World
Ara Pehlivanian
 
JavaScript with YUI
JavaScript with YUIJavaScript with YUI
JavaScript with YUI
Rajat Pandit
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOM
Sukrit Gupta
 

Destacado (20)

Hadoop with Lustre WhitePaper
Hadoop with Lustre WhitePaperHadoop with Lustre WhitePaper
Hadoop with Lustre WhitePaper
 
YUI 3: The Most Advance JavaScript Library in the World
YUI 3: The Most Advance JavaScript Library in the WorldYUI 3: The Most Advance JavaScript Library in the World
YUI 3: The Most Advance JavaScript Library in the World
 
Difference Between DOM and SAX parser in java with examples
Difference Between DOM and SAX parser in java with examplesDifference Between DOM and SAX parser in java with examples
Difference Between DOM and SAX parser in java with examples
 
自在
自在自在
自在
 
用50元買來的CEO
用50元買來的CEO用50元買來的CEO
用50元買來的CEO
 
Big Data and HPC
Big Data and HPCBig Data and HPC
Big Data and HPC
 
逆向思考
逆向思考逆向思考
逆向思考
 
JAXB: Create, Validate XML Message and Edit XML Schema
JAXB: Create, Validate XML Message and Edit XML SchemaJAXB: Create, Validate XML Message and Edit XML Schema
JAXB: Create, Validate XML Message and Edit XML Schema
 
Seagate NAS: Witness Future of Cloud Computing
Seagate NAS: Witness Future of Cloud ComputingSeagate NAS: Witness Future of Cloud Computing
Seagate NAS: Witness Future of Cloud Computing
 
生命是何等美麗 -- 我的好朋友
生命是何等美麗 -- 我的好朋友生命是何等美麗 -- 我的好朋友
生命是何等美麗 -- 我的好朋友
 
JavaScript with YUI
JavaScript with YUIJavaScript with YUI
JavaScript with YUI
 
Personal Professional Development
Personal Professional DevelopmentPersonal Professional Development
Personal Professional Development
 
Java and XML
Java and XMLJava and XML
Java and XML
 
Building Non-shit APIs with JavaScript
Building Non-shit APIs with JavaScriptBuilding Non-shit APIs with JavaScript
Building Non-shit APIs with JavaScript
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
 
Xml processors
Xml processorsXml processors
Xml processors
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript Introduction
 
JAXB
JAXBJAXB
JAXB
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOM
 
XML SAX PARSING
XML SAX PARSING XML SAX PARSING
XML SAX PARSING
 

Similar a The Mysteries Of JavaScript-Fu (RailsConf Ediition)

In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps
In Pursuit of the Holy Grail: Building Isomorphic JavaScript AppsIn Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps
In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps
Spike Brehm
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
Rob Davarnia
 
Welcome to React & Flux !
Welcome to React & Flux !Welcome to React & Flux !
Welcome to React & Flux !
Ritesh Kumar
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
Niti Chotkaew
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
Julien Lecomte
 

Similar a The Mysteries Of JavaScript-Fu (RailsConf Ediition) (20)

The Mysteries Of JavaScript-Fu (@media SF Edition)
The Mysteries Of JavaScript-Fu (@media SF Edition)The Mysteries Of JavaScript-Fu (@media SF Edition)
The Mysteries Of JavaScript-Fu (@media SF Edition)
 
A rough guide to JavaScript Performance
A rough guide to JavaScript PerformanceA rough guide to JavaScript Performance
A rough guide to JavaScript Performance
 
How NOT to get lost in the current JavaScript landscape
How NOT to get lost in the current JavaScript landscapeHow NOT to get lost in the current JavaScript landscape
How NOT to get lost in the current JavaScript landscape
 
GWT is Smarter Than You
GWT is Smarter Than YouGWT is Smarter Than You
GWT is Smarter Than You
 
In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps
In Pursuit of the Holy Grail: Building Isomorphic JavaScript AppsIn Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps
In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps
 
Grokking TechTalk #16: Html js and three way binding
Grokking TechTalk #16: Html js and three way bindingGrokking TechTalk #16: Html js and three way binding
Grokking TechTalk #16: Html js and three way binding
 
Killing the Angle Bracket
Killing the Angle BracketKilling the Angle Bracket
Killing the Angle Bracket
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
 
Presentation of JSConf.eu
Presentation of JSConf.euPresentation of JSConf.eu
Presentation of JSConf.eu
 
Daniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého SchizmaDaniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého Schizma
 
Agile JavaScript Testing
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript Testing
 
Real World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case StudyReal World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case Study
 
Welcome to React & Flux !
Welcome to React & Flux !Welcome to React & Flux !
Welcome to React & Flux !
 
Performance Tuning Web Apps - The Need For Speed
Performance Tuning Web Apps - The Need For SpeedPerformance Tuning Web Apps - The Need For Speed
Performance Tuning Web Apps - The Need For Speed
 
Reactjs Basics
Reactjs BasicsReactjs Basics
Reactjs Basics
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
 
Ajax Performance
Ajax PerformanceAjax Performance
Ajax Performance
 
SoftwareUniversity seminar fast REST Api with Spring
SoftwareUniversity seminar fast REST Api with SpringSoftwareUniversity seminar fast REST Api with Spring
SoftwareUniversity seminar fast REST Api with Spring
 
Introduction to WAMP, a protocol enabling PUB/SUB and RPC over Websocket
Introduction to WAMP, a protocol enabling PUB/SUB and RPC over WebsocketIntroduction to WAMP, a protocol enabling PUB/SUB and RPC over Websocket
Introduction to WAMP, a protocol enabling PUB/SUB and RPC over Websocket
 

Más de danwrong (6)

Loadrunner
LoadrunnerLoadrunner
Loadrunner
 
Bringing the Same-Origin Policy to its Knees
Bringing the Same-Origin Policy to its KneesBringing the Same-Origin Policy to its Knees
Bringing the Same-Origin Policy to its Knees
 
Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rack
 
Taming The Beast
Taming The BeastTaming The Beast
Taming The Beast
 
Metaprogramming JavaScript
Metaprogramming  JavaScriptMetaprogramming  JavaScript
Metaprogramming JavaScript
 

Último

unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
Abortion pills in Kuwait Cytotec pills in Kuwait
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
amitlee9823
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
Renandantas16
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 

Último (20)

The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Service
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communications
 
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Service
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
 
John Halpern sued for sexual assault.pdf
John Halpern sued for sexual assault.pdfJohn Halpern sued for sexual assault.pdf
John Halpern sued for sexual assault.pdf
 
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors Data
 

The Mysteries Of JavaScript-Fu (RailsConf Ediition)