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

14,045 views

Published on

The Mysteries Of JavaScript-Fu presentation as presented at @media Europe in London

Published in: Technology
3 Comments
23 Likes
Statistics
Notes
No Downloads
Views
Total views
14,045
On SlideShare
0
From Embeds
0
Number of Embeds
71
Actions
Shares
0
Downloads
596
Comments
3
Likes
23
Embeds 0
No embeds

No notes for slide

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

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

×