The Mysteries Of JavaScript-Fu (@media SF Edition)

16,262 views

Published on

The version of JS-fu as presented at @media SF. Contains a section on testing and debugging that isn't in the RailsConf version.

Published in: Technology
2 Comments
10 Likes
Statistics
Notes
No Downloads
Views
Total views
16,262
On SlideShare
0
From Embeds
0
Number of Embeds
79
Actions
Shares
0
Downloads
279
Comments
2
Likes
10
Embeds 0
No embeds

No notes for slide

The Mysteries Of JavaScript-Fu (@media SF Edition)

  1. THE MYSTERIES OF JAVASCRIPT-FU Dan Webb (dan@danwebb.net)
  2. About Me JavaScript hacker for ❖ 6 years Ruby/Rails for 2 years ❖ Author of Low Pro, ❖ DOM Builder, Sumo and other stuff Prototype Core Team ❖ http://danwebb.net ❖
  3. A peasant’s language
  4. Web 2.0 has forced ‘real’ programmers to master it
  5. JavaScript-Fu is not easy to master
  6. The web is awash with bad examples and worse resources
  7. Developers forced into refuge behind libraries and frameworks
  8. THE ANCIENT MANUALS OF JAVASCRIPT-FU The Tao Of The Event Handler! ❖ 5 Methods Of DOM Fist! ❖ Lightning Script Style! ❖ The 36 Chambers Of Shaolin! ❖
  9. TRANSLATED... Working with events ❖ Working with DOM elements ❖ Optimisation ❖ Testing and debugging ❖
  10. THE TAO OF THE EVENT HANDLER
  11. The essence of browser scripting is defining behaviour
  12. Big differences in both browser implementation and opinion
  13. The main battle has always been in one area...
  14. INLINE VS SCRIPTED
  15. INLINE EVENT HANDLERS
  16. INLINE EVENT HANDLERS Applied as soon as the browser loads the HTML
  17. INLINE EVENT HANDLERS But what happens when there is more than one....
  18. Bad JavaScript-fu
  19. SCRIPT-BASED EVENT HANDLERS
  20. Attached after element has loaded
  21. Very DRY
  22. Separate JavaScript out in a similar way to CSS
  23. Large numbers of event handlers choke browsers
  24. 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
  25. EVENT BUBBLING <body> handler <div> <p> <a> click
  26. EVENT DELEGATION
  27. BETTER INLINE HANDLERS
  28. BETTER INLINE HANDLERS
  29. 5 METHODS OF DOM FIST
  30. There are 5 methods for updating HTML
  31. 3 official methods (W3C)
  32. $('kungfu').appendChild(node);
  33. $('kungfu').insertBefore(node, child);
  34. $('kungfu').replaceChild(node, child);
  35. 1 non-standard method (you guessed it, from IE)
  36. DOM VS innerHTML
  37. DOM METHODS
  38. Insert elements with precision
  39. But you need to create the nodes first...
  40. ARGH!
  41. LOWPRO’S DOM BUILDER
  42. INNERHTML
  43. Can shift large amount of HTML quickly...
  44. ...but you don't get much control
  45. Incredibly simple to use with Ajax
  46. WHICH WAY? No clear winner ❖ DOM is good for more surgical ❖ manipulation innerHTML is good for replacing large ❖ amounts of content or simple jobs
  47. and the final method...
  48. THE BASTARD SON
  49. LIGHTNING SCRIPT STYLE
  50. ~134KB
  51. 5 HTTP requests
  52. Takes time to download and evaluate script
  53. The less JavaScript the better
  54. Browsers normally only try to load 2 resources concurrently
  55. Combine .js files
  56. Use GZIP compression not JS based minification
  57. Make sure everything is cachable
  58. FASTER LOOPS
  59. FASTER LOOPS
  60. BE CAREFUL WITH SELECTORS
  61. THE 36 CHAMBERS OF SHAOLIN
  62. You're going to need a few tools...
  63. Buy a Mac
  64. Parallels Workstation PARALLELS WORKSTATION
  65. Get Firebug
  66. What about the other browsers?
  67. And then there's testing...
  68. SELENIUM Written by ThoughtWorks ❖ Implemented in HTML/CSS ❖ Test cases define a series of user ❖ actions (using HTML) Assertions check the correct results ❖
  69. LEARNING MORE
  70. LEARNING MORE Dan Webb: http://www.danwebb.net ❖ Low Pro: http://www.danwebb.net/lowpro ❖ Parallels: http://parallels.com ❖ Firebug: http://getfirebug.com ❖ Selenium: http://www.openqa.org/ ❖ selenium/
  71. QUESTIONS?

×