300 likes | 434 Views
Wednesday. Homework 1 = due by email by 4pm Submit to gillw3@rpi.edu (zip your files). Apps v. web apps. Discuss…. JS. Javascript. Javascript. Client Side Scripting Language Object Oriented Event Driven. Places JS Lives. An External File <head> of an HTML file
E N D
Wednesday • Homework 1 = due by email by 4pm • Submit to gillw3@rpi.edu (zip your files)
Apps v. web apps • Discuss….
JS Javascript
Javascript • Client Side Scripting Language • Object Oriented • Event Driven
Places JS Lives • An External File • <head> of an HTML file • In the <body> of an HTML page • (after all the elements it may reference) • JS can be used in multiple locations in the same HTML document
A link to an external javascript document A JS Script coded in the <head> of an HTML document
External JS Files Don’t put another set of <script> tags inside your external JS docs
Variables • All are untyped • Local Variables • Accessible only inside the current function • Global by assignment
Control Structure • If • If / else • Switch • For • While • For (object)
IF • If • If / Else
Loops • For • While
Functions • Declaring a Function • Calling a Function
Events http://www.w3schools.com/jsref/jsref_events.asp
Timer Events • Run JS in the future • setTimeout(“js code”, milliseconds); • Code must be in quotes!!!!!!!!! • setTimeout(“print_time()”,1000);
Timer Events • Do every N seconds: Function do(){ ….. Code; setTimeout(“do()”,1000); }
JS Objects • Properties • A value held by an object • Methods • Act upon an object A string property A string method
String Methods • str.substr(start, length) • Returns a sub string • str.indexOf(seach) • Returns position of the first instance of search • str.replace(bad, good) • Replaces bad text with good text • str.split(delimiter) • Breaks string into an array at every instance of delimiter
String Properties • str.len • Returns length of a string
Math Methods • Math.ceil(x) • Rounds up to the next integer value • Max(x,y) • Picks the larger of the two arguments • Math.random() • Returns a random number between 0.0 and 1.0
Document Object • Properties • Document.URL • Returns current url and query string • Methods • document.writeln() • Prints a line in the current document • Document.getElementById(id) • returns an HTML element with the ID you provide
Debugging • Web dev tool bar: • Click the red exclamation marks to see errors • alert(somevar); • alert(‘working at line 36’);
In Class • Write a ‘hello world’ script put it inside the body of an HTML document • View the generated html … what’s happened here? • Write a ‘hello world’ function – call it on body onload • View the generated html … what’s happened here?
In Class • Write a script that generates the following text: 1+1, 1+2, …., 1+10 • If the user clicks on one of these items, produce an alert box with 1+1 = 2 (for example)