Javascript Client-side scripting
Up to now We've seen a little about how to control content with HTML presentation with CSS Javascript is a language that can be used to control browser actions
Javascript language General-purpose language designed to be embedded within other applications C-like syntax Object-oriented Interpreted Built-in garbage collection Support for regular expressions Built-in security - can't read local files or do networking
What JavaScript can do Control document appearance and content Control the browser Interact with forms Validate user input Interact with user Use cookies
JavaScript is several languages JavaScript - implemented by Netscape (and later Mozilla) Jscript - Microsoft version ECMAScript -standardized version of the language international.org/publications/standards/Ecma-327.htm JavaScript is not related to Java
How to use JavaScript JavaScript can be embedded into your html pages in a couple of ways in elements in both and elements of page in event attributes of appropriate elements You can also reference external files containing JavaScript code
Basic Syntax Case-sensitive Insensitive to whitespace ; line terminator is optional (unless result is ambiguous) // and /* … */ comments Same rules for identifiers as Java
Variables Must be declared before they can be used var i; var x, y; using undeclared variable causes an implicit declaration - resulting variable is global variables declared in functions are local Untyped can be assigned to values of any type
Types Primitive types numbers strings booleans - true or false &&, || and ! null undefined Reference types objects: unordered collection of named values arrays: ordered collection of unnamed values functions special classes Date RegExp
Numbers Numbers - all stored using IEEE bit standard Literals integer : Hex : 0xff 0xCAFE911 Octal : 0377 Floating point : e23 1.6e-19 Operations: standard arithmetic, functions of Math object, relational operators, bitwise operators Special values: Infinity, NaN, Number properties
Strings Sequence of Unicode characters Literals can be enclosed in either single or double quotes Escape sequences include the ones you are used to plus the ability to represent a character by its Unicode ( \uXXXX ) Concatenate with +
String properties and methods length - the number of characters in the string charAt( index) - index from 0 substring( start, endPlus1) use + to concatenate strings
Functions A piece of executable code First-class objects Definition function square(x) {return x*x; } Literal functions var square = function(x){return x*x; } var square = new Function( "x", "return x*x;”);
Objects An object is collection of named values which are referred to as the properties of the object property that is a function is called a method Objects are created with new var o = new Object() Object literals var point = {x:2.3, y:-1.2}
Using Objects Once the object is created, you can add and use whatever properties you need var point = new Object(); point.x = 2.3; point.y = -1.2 Use the in operator to check if an object has a particular property var hasXcoord = "x" in point;
Arrays Arrays are indexed collections of data values size is dynamic Creating arrays var a = new Array(); // now add elements a[0] = 1.2; a[1]="JavaScript"; … var b = new Array( 1.2, "JavaScript", true, …} var c = new Array(5); //5-element array Array Literals var a = [1.2, "JavaScript", true, {x:1, y:3}]
Statement Types Expression statements have side effects Assignment - both simple ( = ) and compound ( +=, -=, …) delete function calls (with no return value) Compound statements - enclosed in { } no block scope in JavaScript var for declaring variables
Selection if if (cond1) stmt1; elseif (cond2) stmt2; … else stmtLast; switch switch (n) { case 1: block1; break; … default: defaultBlock; }
Repetition while while (cond) stmt; do/while do stmt; while (cond); for for (init; test; update) stmt; for/in for (var in obj) stmt; loops through elements of array or properties of object
More statement types function return throw try/catch/finally with - used to temporarily modify scope chain ; is empty statement
Sources Web Design and Programming by Paul S. Wang and Sanda S. Katila JavaScript The Definitive Guide by David Flanagan dbook/javascript/ dbook/javascript/ international.org/publications/standards/Ecma- 262.htm