The Mysteries Of JavaScript-Fu (RailsConf Ediition)

13,070 views

Published on

Dan Webb's 'The Mysteries Of JavaScript-Fu' as presented at RailsConf 2007.

Published in: Business, Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
13,070
On SlideShare
0
From Embeds
0
Number of Embeds
83
Actions
Shares
0
Downloads
429
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

The Mysteries Of JavaScript-Fu (RailsConf Ediition)

  1. 1. THE MYSTERIES OF JAVASCRIPT-FU Dan Webb (dan@danwebb.net)
  2. 2. “Ah, JavaScript, the language we all love to hate” Geoffrey Grosenbach, Peepcode
  3. 3. A peasant’s language
  4. 4. Web 2.0 has forced ‘real’ programmers to master it
  5. 5. JavaScript-Fu is not easy to master
  6. 6. The web is awash with bad examples and worse resources
  7. 7. Developers forced into refuge behind libraries and frameworks
  8. 8. THE ANCIENT MANUALS OF JAVASCRIPT-FU The Tao Of The Event Handler! ❖ 5 Methods Of DOM Fist! ❖ Lighting Script Style! ❖ Iron Ajax Technique! ❖
  9. 9. TRANSLATED... Working with events ❖ Working with DOM elements ❖ Optimisation ❖ Progressive enhancement ❖
  10. 10. THE TAO OF THE EVENT HANDLER
  11. 11. The essence of browser scripting is defining behavior
  12. 12. Big differences in both browser implementation and opinion
  13. 13. The main battle has always been in one area...
  14. 14. INLINE VS SCRIPTED
  15. 15. INLINE EVENT HANDLERS
  16. 16. INLINE EVENT HANDLERS Applied as soon as the browser loads the HTML
  17. 17. INLINE EVENT HANDLERS But what happens when there is more than one....
  18. 18. Bad JavaScript-fu
  19. 19. SCRIPT-BASED EVENT HANDLERS
  20. 20. Attached after element has loaded
  21. 21. Very DRY
  22. 22. Separate JavaScript out in a similar way to CSS
  23. 23. Large numbers of event handlers choke browsers
  24. 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. 25. EVENT BUBBLING <body> handler <div> <p> <a> click
  26. 26. EVENT DELEGATION
  27. 27. BETTER INLINE HANDLERS
  28. 28. BETTER INLINE HANDLERS
  29. 29. 5 METHODS OF DOM FIST
  30. 30. There are 5 methods for updating HTML
  31. 31. 3 official methods (W3C)
  32. 32. $('kungfu').appendChild(node);
  33. 33. $('kungfu').insertBefore(node, child);
  34. 34. $('kungfu').replaceChild(node, child);
  35. 35. 1 non-standard method (you guessed it, from IE)
  36. 36. DOM VS innerHTML
  37. 37. DOM METHODS
  38. 38. Insert elements with precision
  39. 39. But you need to create the nodes first...
  40. 40. ARGH!
  41. 41. LOWPRO’S DOM BUILDER
  42. 42. INNERHTML
  43. 43. Can shift large amount of HTML quickly...
  44. 44. ...but you don't get much control
  45. 45. Incredibly simple to use with Ajax
  46. 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. 47. and the final method...
  48. 48. THE BASTARD SON
  49. 49. LIGHTENING SCRIPT STYLE
  50. 50. ~134KB
  51. 51. 5 HTTP requests
  52. 52. Takes time to download and evaluate script
  53. 53. The less JavaScript the better
  54. 54. Browser normally only try to load 2 resources concurrently
  55. 55. Combine .js files
  56. 56. Use GZIP compression not JS based minification
  57. 57. Make sure everything is cachable
  58. 58. FASTER LOOPS
  59. 59. FASTER LOOPS
  60. 60. BE CAREFUL WITH SELECTORS
  61. 61. IRON AJAX TECHNIQUE
  62. 62. RULE #1: BROWSERS SUCK
  63. 63. Main browsers are getting better quickly
  64. 64. But what about the others?
  65. 65. Corporate security and firewalls often block JavaScript
  66. 66. The traditional answer from Rails:
  67. 67. But why turn users away if you don’t have to?
  68. 68. 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
  69. 69. It's easy to apply this to Ajax
  70. 70. IRON AJAX action.rhtml respond_to :html POST Controller render :partial _partial.rhtml
  71. 71. IRON AJAX action.rhtml respond_to :html POST Controller render :partial Ajax POST respond_to :js _partial.rhtml
  72. 72. IRON AJAX action.rhtml respond_to :html POST Controller render :partial Ajax POST respond_to :js action.rjs
  73. 73. THE HTML
  74. 74. THE HTML
  75. 75. THE JAVASCRIPT
  76. 76. THE CONTROLLER
  77. 77. THE CONTROLLER
  78. 78. Easy
  79. 79. I know what you’re thinking
  80. 80. But it won’t work if...
  81. 81. Try progressive enhancement first
  82. 82. LEARNING MORE Dan Webb: http://www.danwebb.net ❖ Low Pro: http://www.danwebb.net/lowpro ❖
  83. 83. LEARNING MORE
  84. 84. QUESTIONS?

×