Javascript & jQuery

CDGD304 - Fall 2011
http://bit.ly/massart-304-8

HTML/CSS Are Static

(Now only mostly true, as you can add interactivity via CSS Hover, Animation)

Anything else dynamic is "programmed"

And, unless it's flash..

It's programmed in Javascript
"Lingua Franca" of the Web

Disclaimer:

Programming is Hard

We'll do our best..

But you're not going to learn it in a day. It take a lot longer than that...

About Javascript

Javascript is both:

Why so much hate for JavaScript?

What Changed?

Where does Javascript go?

Similar to CSS - multiple options (notice: type='text/javascript' NOT required in HTML5):

Where do we prefer?

Also Similar to CSS - the less 'inline' the better. (Generalization)

Again, we're not really teaching "programming"

There's way too much to try to cover in 1 class

We're really want to be able to use jQuery

And need just enough Javascript to get by.

3 Main Goals for today

  1. Be able to read and understand some JavaScript

  2. Be able to manipulate documents with jQuery

  3. Be able to use jQuery plugins

What is jQuery?

What pieces of Javascript do we need to understand?

A Little Demo

The goal is to understand everything in here by the end of class today

Javascript Housekeeping

Hello World!

Call the built in 'alert' function and have it say "Hello World!" We don't use this all that often (it's ugly) - but it's really useful for debugging.

Our friend the console and console.log

Puts a result in the console - use Chrome or Firebug (must be open)

What is a variable?

Variables

Let's do some math and some string manipulation and write out to the console.

Scope

You can only use variables that are defined in the scope you call them.

Functions

Created with the "function" keyword, create reusable pieces of functionality.

Returning from Functions

Use the return keyword to pass a value back to whatever called the function

Timeouts

we can use setTimeout to have something happen later, try it

Anonymous functions, callbacks

We don't have to give our callback function a name, we can do it anonymously

Why callbacks?

A lot of stuff on the web happens "Asynchronously" - AJAX, user actions, etc

Arrays

Playing with arrays

Objects

Objects

Conditionals

Conditional Exercise

Use a conditional to check if a user confirmed a ok/cancel question. Then use prompt and test against a return value.

jQuery Basics

Docs are your Friend

Get comfortable with docs.jquery.com

Loading jQuery

Include from a CDN or locally

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>

Or

<script src='js/jquery.min.js'></script>

Need to make sure we have the file in the right location, can view in browser if there's a problem.

Using jQuery

Since we often include Javscript in the head of the document, we need to make sure we wait until the page is loaded before we use it. We can use the jQuery bind function to load stuff at the appropriate point.

<script>
$(document).bind("ready",function() {
  // Dom Loaded
});
</script>

Why this is important..

Shorthand

Since this is so common, we can use a shorthand for this of $(function() { }) ...

<script>
$(function() {
  // Dom Loaded
});
</script>

Basic selection + manipulation

Let's try out $.text, $.html, $.hide, $.show, $.css, $.attr, $.addClass, $.hasClass, $.removeClass Selector Docs / Manipulation Docs

Some basic DOM insertion

Let's try out creating elements with $(), then using $.before, $.after, $.append, $.prepend, $.wrap, $.appendTo, both with text and elements.

Fun Stuff: Animation and transforms

$.fadeOut, $.fadeIn, $.slideDown, $.slideUp, $.toggle, $.stop, $.animate

Events

$.bind, $.click, $.dblclick, $.mouseenter, $.mouseleave, $.hover

Demo Revisited

The goal is to understand everything in here by the end of class today

Putting it all together

Let's use two buttons to increase and decrease a counter.

Plugins: Barebones

Let's use the cycle plugin: http://jquery.malsup.com/cycle/ (images from http://lorempixel.com)

Cycle: Style and Options

Ajax

$.ajax, $.get, $.post, $.getJSON

JSONp

Ajax is same-domain only. So there's a hack to pull data across domains called $.getScript

Homework

Coding