JavaScript: JS301

16
JavaScript: JS301 Week 2: Advanced Functions, the DOM & jQuery

description

JavaScript: JS301. Week 2: Advanced Functions, the DOM & jQuery. More on Functions. Functions can be assigned to variables! http://jsfiddle.net/zymsys/REu39/. More on Functions. Functions don’t need names! http://jsfiddle.net/zymsys/B2Yn4/. Variable Scope. - PowerPoint PPT Presentation

Transcript of JavaScript: JS301

Page 1: JavaScript: JS301

JavaScript: JS301Week 2: Advanced Functions, the DOM &

jQuery

Page 2: JavaScript: JS301

More on Functions

•Functions can be assigned to variables!

•http://jsfiddle.net/zymsys/REu39/

Page 3: JavaScript: JS301

More on Functions

•Functions don’t need names!

•http://jsfiddle.net/zymsys/B2Yn4/

Page 4: JavaScript: JS301

Variable Scope

•Up until now our variables have all been global, that that’s BAD!http://jsfiddle.net/zymsys/UnZe8/

•We can solve this by giving our variables local scope:http://jsfiddle.net/zymsys/d2NWX/

Page 5: JavaScript: JS301

Variable Scope

•Variables which aren’t defined with ‘var’ are automatically global, which is BAD.

•So ALWAYS declare your variables with ‘var’.

•Wrapping code in a function is another way to avoid placing anything in the global scope.

Page 6: JavaScript: JS301

Closures

•Variables in JavaScript are visible within the closure they are defined in.

•Closures are usually functions

•http://jsfiddle.net/zymsys/Lu8Kz/

Page 7: JavaScript: JS301

Parameters are References

•When you pass a parameter to a function you pass a reference to it, not a copy of it.

•http://jsfiddle.net/zymsys/tL9GX/

Page 8: JavaScript: JS301

The Document Object Model

(DOM)•The DOM is an object oriented

interface which allows JavaScript to interact with web content.

•http://jsfiddle.net/zymsys/xef4K/

•http://jsfiddle.net/zymsys/MDTb8/

Page 9: JavaScript: JS301

Working with the DOM is Difficult

•In the last example we saw how difficult it can be to do something as simple as tweak an element’s style

•There’s a lot of that to deal with, and the cross-browser issues that follow

•This is why libraries like jQuery are so popular

Page 10: JavaScript: JS301

jQuery

•Here’s the same example with jQuery:

•http://jsfiddle.net/zymsys/JmdMM/

Page 11: JavaScript: JS301

Popular Libraries

•http://jquery.com/

•http://developer.yahoo.com/yui/

•http://dojotoolkit.org/

•http://mootools.net/

These are just a few of the more popular libraries:

Page 12: JavaScript: JS301

jQuery FTW!•jQuery is very popular:

•Easy to find solutions with Google

•Lots of plugins and examples

•Lots of other libraries already depend on jQuery

•jQuery is expressive and relatively easy

Page 13: JavaScript: JS301

jQuery Events

•Here’s an update to our jQuery example:

•http://jsfiddle.net/zymsys/JmdMM/3/

•No impact on our global namespace!

Page 14: JavaScript: JS301

jQuery Animation

•jQuery makes animation easy, just don’t get carried away!

•http://jsfiddle.net/zymsys/FaB2f/

Page 15: JavaScript: JS301

jQuery UI

•User interface widgets and visual effects

•Plenty of demos on the jQuery UI site:

http://jqueryui.com/demos/

Page 16: JavaScript: JS301

Next Week

•Practical JavaScript

•Form Validation

•AJAX

•Rich User Interfaces

•Requests?