JavaScript, AJAX, Mashups, and Web 2.0

March 20, 2007

Harvard University
Division of Continuing Education
Extension School

Course Web Site: http://cscie12.dce.harvard.edu/

Copyright 1998-2007 David P. Heitmeyer

Instructor email: david_heitmeyer@harvard.edu
Course staff email: cscie12@fas.harvard.edu


Add your location to the map. All information is optional, and is restricted to class members. This is an illustration of using the power of Google Maps API. Provide your location in the next week (Tue March 27), and you will earn 10 points towards your 'participation' grade.

Introduction to JavaScript, AJAX, and Mashups

Experience

Introduction to JavaScript, AJAX, and Mashups

Technology

Example of JavaScript

Let's take a look at checking form input with JavaScript, Example:

The important pieces:

  1. Reference an external javascript file. The javascript functions in this file can now be used within the page.
  2. The "example7.js" contains a function ValidateForm. The ValidateForm function checks to see if the email input box is not empty and if the string matches the format of an email address.
  3. onsubmit attribute of form element. This is an "event attribute".

JavaScript Objects

JavaScript is an object-oriented scripting language. The dot notation is used to access properties or invoke methods of the object.
Objects
Examples: Object: Properties Object: Methods

Event Attributes

These are attributes defined for X/HTML elements, which are "event attributes":

JavaScript and XHTML

To function in all browsers, script element should not be empty, but should include a space, a comment, or even a simple return true; statement.

JavaScript in XHTML/HTML

JavaScript Examples: Forms

Calculation with onclick

Two javascript statements:

Example 8.1
Example 8.1 Source:
In example1.js
In head element:
Example 8.1 Demonstrated

Calculation with onblur

Example 8.2
Example 8.2 Source:
In example2.js
In head element:
Example 8.2 Demonstrated

Redirect onclick

window.location = menu.options[menu.selectedIndex].value;
Example 8.3
Example 8.3 Source:
In script element (within head element):
Example 8.3 Demonstrated

Redirect onchange

Example 8.4
Example 8.4 Source:
In script element (within head element):
Example 8.4 Demonstrated

Form Control

Validate form input using the onsubmit event handler.

Example 8.5
Example 8.5 Source:
In script element (within head element):
Example 8.5 Demonstrated

Form Validation and Regular Expressions

Validate form fields with Regular Expressions

Regular Expression:
/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/

Example 8.6
Example 8.6 Source:
In example6.js
In head element:
Example 8.6 Demonstrated

Validation onsubmit

Example 8.7
Example 8.7 Source:
In example7.js
In head element:
Example 8.7 Demonstrated

Form Validation: Resources

Tutorials

Libraries and Examples

Search Box Example

Example 8.8
Example 8.8 Source:
Example 8.8 Rendered:

Body onload and focus

Body onload and focus on a form input.

Use of :focus pseudo-class to style those input boxes with focus.

Web 2.0

What it isn't

Buzzword or something "real"?

Web 2.0 Features and Characteristics

What Is Web 2.0: Design Patterns and Business Models for the Next Generation of Software by Tim O'Reilly

Web 1.0 and 2.0 Examples

Principles, Characteristics, Features:


Web 2.0 in Images

Web 2.0 Meme Map by Tim O'Reilly
web20mememap

Web 2.0 in Images

Visualizing Web 2.0 by Dion Hinchcliffe
web2.0

Web 2.0 in Images

Visualizing Web 2.0, Peter, blog.forret.com
Web 2.0 Overview

Zillow

zillow
www.zillow.com

Housingmaps.com

housing maps
www.housingmaps.com

chicagocrime.org

chicagocrime.org
www.chicagocrime.org

del.icio.us

del.icio.us
del.icio.us

flickr

flickr
flickr.com

WorldCat

WorldCat[OCLC]

"Weaving the Web" by Tim Berners-Lee, ISBN: 006251587X

Link to this book in WorldCat is simply: http://worldcat.org/isbn/006251587X

WorldCat

Amazon Web Services

Amazon.com offers several different web services, including one for e-commerce.

http://www.amazon.com/gp/product/006251587X%3ftag=webservices-20%26link_code=xm2%26camp=2025%26dev-t=DEVELOPER_TOKEN

Amazon WS

Google News

google news

Rich Internet Applications

Rich Internet Applications, Rich User Experience

gmail
gmail.com


google maps
maps.google.com


google calendar
calendar.google.com


google documents
docs.google.com

DOM and XHTML/HTML

W3C XML DOM allows the all aspects of the document to be accessed and manipulated.

getElementById is a frequently seen method

Once we have the node selected, we can manipulate its properties. In this case, we are changing the "visibility" to "visible" or "hidden".

Showing and Hiding Menus, part 1

Example 8.9
Example 8.9 Source:
Example 8.9 Demonstrated

Showing and Hiding Menus, part 2

Block whose visibility is set can be positioned absolutely.

Example 8.10
Example 8.10 Source:
Example 8.10 Demonstrated

Another example of Simple DOM manipulations

We can show, hide, and change the style properties via Javascript

Example 8.11
Example 8.11 Source:
In example11.js
In head element:
Example 8.11 Demonstrated

Exposing Additional Form Elements

Example 8.12
Example 8.12 Source:
In script element (within head element):
Example 8.12 Demonstrated

Your Work Easier with JavaScript Libraries

Tables: Class gives Functionality

Tables are constructed with thead and tbody. Simply by giving them a class="sortable", the jQuery Tablesorter Plugin makes them 'sortable' and 'striped':

Senate data comes from GovTrack.US. Table sorting is achieved through jQuery and the jQuery Tablesorter Plugin

Sortable Tables

Tables are constructed with thead and tbody. Simply by giving them a class="sortable", the jQuery Tablesorter Plugin makes them 'sortable' and 'striped':

The JavaScript and CSS:

table table_sortable

Senate data comes from GovTrack.US. Table sorting is achieved through jQuery and the jQuery Tablesorter Plugin

Code Syntax Highlighter

Using the dp.SyntaxHighlighter, plain textarea elements can be transformed into highlighted code.

What is needed:

With and Without

And here is the same code, without the SyntaxHighlighter invoked:

AJAX

Asynchronous JavaScript and XML

AJAX: A New Approach to Web Applications, from Adaptive Path. ajax by Jesse James Garrett

AJAX: Asynchronous JavaScript and XML


Technologies Involved

AJAX

ajax schematic

Google Maps

Place "markers" and information on maps via XML data.

CSCI E-12 Campus Map
cscie-153 campus map

Marker Data

campus_data.xml
campus_data.xml

Javascript createMarker function:

Ajax Example with Prototype

Prototype is a JavaScript/AJAX library that greatly simplifies creating and using AJAX.

ajax update

There is a URL that returns a list of courses given a course group (as an XHTML snippet):

What's involved:

Javascript:

Invoking the Javascript:

Periodic Updater with Prototype

Autocomplete Text Fields

autocomplete

There is a URL that returns a list of course titles given a string (as an XHTML list):

Tabs with jQuery

Tab Plugin for jQuery allows the easy creation of tabs.

Exhibit

Exhibit is a lightweight structured data publishing framework that lets you create web pages with support for sorting, filtering, and rich visualizations by writing only HTML and optionally some CSS and Javascript code.

exhibit home

Validation of College and University Web Sites

Collected website information from over 250 colleges (list from US News & World Report).

For each school, collect the following information:

URL http://www.washington.edu/
Title University of Washington
Valid valid
Errors 0
Doctype -//W3C//DTD XHTML 1.0 Strict//EN
encoding iso-8859-1
error string 0 error
URL_end http://www.washington.edu/
thumshot_url http://open.thumbshots.org/image.pxf?url=http://www.washington.edu/

This was accomplished through the wonders of Perl and the xml output of the W3C Markup Validator

Note too that we are using Open Thumbshots, which allows us to grab a 120x90 pixel image of a URL.
thumbshot

About Thumbshots thumbnails

Data into JSON

Convert the data to "JSON" format (JSON is Javascript Object Notation).

One entry looks like:

The entire data file: colleges.json

And now for Exhibit

College in Exhibit

Steps required:

  1. Import the Exhibit javascript
  2. Reference the data
  3. Create templates

Exhibit Templates

college exhibit

Template that defines a view:

Other Exhibit Components

college exhibit

Add Another View

college exhibit

Getting the Data

United States Senate

senate senate senate

Senate data comes from GovTrack.US.