CSSE 290 Web Programming

Lecture 14: JavaScript Language Features

Reading: 8.1 - 8.4

Attribution:Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

Otherwise noted: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller. The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.
Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them. My intention is to mark these examples with [DSF].

Valid HTML Valid CSS!

Announcements

JavaScript "strict" Mode

screenshot
"use strict";

your code...

Special Values: null and undefined

var ned = null;
var benson = 9;
var caroline;

// at this point in the code,
//   ned is null
//   benson is 9
//   caroline is undefined

Recap: Splitting Strings: split and join

var s = "the quick brown fox";
var a = s.split(" ");          // ["the", "quick", "brown", "fox"]
a.reverse();                   // ["fox", "brown", "quick", "the"]
s = a.join("!");               // "fox!brown!quick!the"

More about JavaScript functions

For this excursion, many of the ideas and much of the code come from Chapter 3 of Secrets of the JavaScript Ninja by John Resig and Bear Bibeault, ©2013 by Manning Publications.

Download and unZIP the code, linked from today's session on the schedule page.

JavaScript functions are "first-class" data, unlike Java methods.

An Advantage of First-class Functions

Simplicity of expression. Example: sort an array into reverse order.
First, in Java:

Integer[] values = {213, 16, 297, 34, 10, 512, 57};
java.util.Arrays.sort(values, new Comparator(){
  public int compare(Integer value1, Integer value2) {
	return value2 - value1;
  }
});

Now, in JavaScript:

var values = [213, 16, 297, 34, 10, 512, 57];
values.sort(function(value1, value2) { return value2 - value1; });

We'll see more about how this works in JavaScript later. Which appears to be more straightworward?

Java 8 now has (sort of) first-class functions.

Parts of a JavaScript function literal

An anonymous function is not given a name; its name is the empty string.

Let's examine and run the listing-3.1.html file from Ninja Chapter 3

JavaScript Scope Rules

An anonymous function is not given a name; its name is the empty string.

Example (from Ninja book)

	function outer(){
       var a = 1;
        function inner(){ /* does nothing */ }
        var b = 2;
        if (a == 1) {
          var c = 3;
		}
     }

    outer();

Let's examine and run listing-3.2.html from Ninja Chapter 3

Array of functions example

Because we can!

Uses assert fomr the Ninja book

	var functionArray = [];
	for (var i=0; i<10; i++) {
		functionArray[i] = function(k) {return k*i; };
	}
	resultsArray = [];
	for (i=0; i<10; i++) {
		resultsArray[i] = functionArray[i](3);
	}
	assert(true, resultsArray.join(" "));
	

output from above code

9.2: DOM Element Objects

Recap: DOM Element Objects

dom object

DOM Object Properties

<div id="main" class="foo bar">
	<p>Hello, <em>very</em> happy to see you!</p>
	<img id="icon" src="images/borat.jpg" alt="Borat" />
</div>
var mainDiv = document.getElementById("main");
var icon    = document.getElementById("icon");
Property Description Example
tagName element's HTML tag mainDiv.tagName is "DIV"
className CSS classes of element mainDiv.className is "foo bar"
innerHTML content in element mainDiv.innerHTML is "\n <p>Hello, <em>ve...
src URL target of an image icon.src is "images/borat.jpg"

Modifying Text Inside An element

var paragraph = document.getElementById("welcome");
paragraph.innerHTML = "Welcome to our site!";  // change text on page

DOM element objects have the following properties:

Abuse of innerHTML

// bad style!
var paragraph = document.getElementById("welcome");
paragraph.innerHTML = "<p>text and <a href="page.html">link</a>";

Adjusting styles with the DOM

<button id="clickme">Color Me</button>
window.onload = function() {
	document.getElementById("clickme").onclick = changeColor;
};
function changeColor() {
	var clickMe = document.getElementById("clickme");
	clickMe.style.color = "red";
}
Property Description
style lets you set any CSS style property for an element

Common DOM styling errors

DOM properties for form controls

<input id="sid" type="text" size="7" maxlength="7" />
<input id="frosh" type="checkbox" checked="checked" /> Freshman?
var sid   = document.getElementById("sid");
var frosh = document.getElementById("frosh");
Property Description Example
value the text/value chosen by the user sid.value could be "1234567"
checked whether a box is checked frosh.checked is true
disabled whether a control is disabled (boolean) frosh.disabled is false
readOnly whether a text box is read-only sid.readOnly is false

More about form controls

<select id="captain">
	<option value="kirk">James T. Kirk</option>
	<option value="picard">Jean-Luc Picard</option>
	<option value="cisco">Benjamin Cisco</option>
</select>
<label> <input id="trekkie" type="checkbox" /> I'm a Trekkie </label>

Extended Example: flexible 15-puzzle

15-puzzle picture