Introduction to the
 wonderful world of JS.
• JavaScript - pitfalls and gotchas
• jQuery - a JavaScript library
• Examples and questions
Jakob Torp

• @googletorp
• Work in Reveal IT - mostly Drupal
  developer/consultant.
• Drupal 1.3 years
JavaScript
pitfalls and gotchas
JavaScript OO
   programming language
• Object oriented.
• No relations to Java. (LiveScript)
• Purpose is to make the browser interactive.
• Created by NetScape in 2 weeks!
• Reverse engineered by Microsoft. (browser war)
• Official name: ECMAScript.
JavaScript happens in
    the browser.
Boolean
type conversions
Object declarations.



Last comma causes syntax error in IE
Function declarations.
Functions can be
passed as variables.
Functional scope
Closure.
jQuery
a JavaScript library
Why use a library?

• Development framework like Drupal.
• Reuse code - faster development.
• Good documentation.
• Help solve browser issues.
• Help optimising your code.
jQuery motto
  “write less do more”
• DOM - Document Object Model.
• CSS selection - fast and easy (Sizzle)
• Event handlers - click, focus etc.
• AJAX & animations
• Plugins - works like a Drupal module.
DOM
CSS selection




     VS:
Events & eventhandlers

• Events - user interaction:
  click, focus, blur ect.
• Event-handlers - Execute code for specific
  events. (function).
jQuery chaining
jQuery returns jQuery
Ajax simplified
jQuery plugins


Flot module
drupal_add_js()

• Add your own js file on a given page.
• Add PHP variables to the global Drupal
  variable.
• Possible to set scope and other options.
• Edge case, drupal_get_js()
Examples & questions

Introduction to the wonderful world of JavaScript