Web Programming/JavaScript

Resources:
 * slides on JavaScript
 * slides on jQuery
 * JavaScript tutorials on w3schools.com
 * JavaScript references on w3schools.com
 * JQuery tutorials on w3schools.com
 * JQuery references on w3schools.com
 * jsfiddle
 * /JQuery/
 * JavaScript: Understanding the Weird Parts - The First 3.5 Hours

JavaScript is a scripting language. When attached to a webpage (HTML) it is interpreted by a web browser and can be used to manipulate the webpage by responding to various events. Traditionally JavaScript runs on the client site to create dynamic behaviors, such as form validation, to make a page more interactive. Recently JavaScript is increasingly being used to make asynchronous web requests (ajax) and handle web requests on the server (node.js).

=Life Cycle=

A JavaScript program is a plaintext file with JavaScript code. When the script file (code) is loaded a JavaScript interpreter parses statements in the file sequentially and carry out the operations. If a statement defines a function the interpreter remembers the definition and only executes the function when it is called. The following JavaScript code defines one function, which uses the alert function to say "Hello". When the script is loaded it executes one statement, which invokes the sayHello function, and then it executes what is in the function definition.

A client-side JavaScript file is always attached to an HTML file, which allows the script access to the object (DOM) that represents the page. Often times the first thing we do in the script is to attached functions to the page elements to handle various events. The following HTML code defines a button and includes a demo.js to make the page interactive by handling events.

The demo.js file has the following content. In the script the implicit window object is used to attached a event handler to the onload event, which takes place when whole web page finishes loading. The event handler is defined as an anonymous function, which is fine because the function is only referenced once. In the function we use the implicit document object (representing the page) to find the button object by its ID and attached the sayHello function to handle the button's click event. Note that the first three lines of code is a single statement, which is why it needs to be terminated with ";". The anonymous function is not executed till the whole page is loaded and the sayHello function is not executed till the button is clicked. We have to wait for the page to finish loading before we can attached event handlers to page elements because the elements have to exist before we can reference them.

The script can be included in the HTML directly as follows. This is OK, but it doesn't cleanly separate the content (HTML) and the behavior (JavaScript) and hinders code reuse (you can not use the script on other pages). I will use this style in future examples because the code stays in one file in compact form.

=DOM= The "document" in the previous example points to a DOM (Document Object Model) object that represents the webpage. In fact, before a browser draws a page on the screen it has to download the HTML code that defines the page, and then constructs a DOM object in memory for the page. So the DOM object for a page is the underlying data structure for rendered page on the screen. We can modify the page on screen by changing its DOM object, for example modifying the content or the style of an element, adding a new element, and removing an element.

In the next example we say hello by changing the content of a paragraph instead of using a popup window, which can be disturbing. The paragraph element has property called innerHTML representing its content. We can change this property to change the content of the paragraph on the web page.

=Events= In addition to the onclick event there are many other events we can choose to handle. Most of the events are triggered by user actions. Sometimes we want to programmatically trigger a event with a delay or periodically. The following example uses a timer to delay the calling of a function. The setTimout function takes two parameters: the first one is the name of function we want to call when the timer expires and the second parameter specifies the delay in milliseconds. It would be more interesting to animate the count down on the page, which means we need to update the content of the paragraph periodically. We can set the timer with an interval and clear it when the count reaches zero. This idea is demonstrated in the next example.

We can build a complete stop watch using this idea.

=jQuery= jQuery is a JavaScript library that extends the features of "native" JavaScript. It is the most popular JavaScript library because it
 * simplifies DOM manipulation,
 * allows element selection via CSS selector,
 * addresses browser incompatibility issues,
 * offers functional programming abilities (map an operation to a set of elements),
 * offers method chaining by returning the caller object from most functions.

Syntax
Simple syntax rules can be found at
 * basics
 * statements

Common utility methods include
 * string methods
 * array methods
 * math methods
 * date/time methods
 * regular expression

Some interesting constructs include:
 * for each loop
 * toArray method
 * method chaining

is a shorthand expression for Any code that depends on a fully loaded page needs to go into this function.

The following is called a immediate function that run immediately without waiting for the document to become ready: a function is defined and called upon in a single statement.

Element Selection
jQuery provides a much richer API for selecting HTML elements in almost all possible ways you can imagine. Most selectors are described at http://www.w3schools.com/jquery/jquery_selectors.asp and you can find a comprehensive demo at http://www.w3schools.com/jquery/trysel.asp

A jQuery selector finds/selects a number of HTML elements, on which we can apply operations.

DOM Manipulation
jQuery makes DOM manipulation easier by providing a rich set of methods we can use. The following are the main categories of such methods:
 * get contents and attributes: with multiple elements selected the method concatenate the contents/attributes and return a single string.
 * set contents and attributes: we can set the same value to all selected elements or use a callback function to set different values based on the indices of the elements in the selected elements.
 * add new contents or elements: the $(' ') method can create an element with any tag name.
 * remove contents or elements: a selector can be used to filter the elements to be removed.

The following example generates a new square (div) and added to a parent container when the button is clicked.

CSS Manipulation
The ideal way to manipulate style information is to define CSS classes and change the styles of elements by adding them to a class or remove them from a class: get and set CSS classes.

jQuery also provides a general-purpose .css function for manipulating individual CSS properties directly: css method. When the css method is called with one parameter, it gets a property with the same name as the parameter. When it is called with two parameters, it sets the property with the value as the second parameter. When the css method is called on a set of selected elements it behaves differently depending on whether it is a get or a set: get returns the property value of the FIRST selected/matched element and set changes the property values of ALL selected elements.

Event Handling
jQuery is designed to respond to all kinds of events on an HTML page.
 * events
 * event methods and attributes

Effects
The following effects can be achieved using jQuery:
 * hide and show
 * fade
 * sliding
 * animation

jQuery UI
jQuery UI is a collection of GUI widgets built with jQuery, HTML, and CSS. A getting started guide can be found on jQuery learning center.