Javascript What is Java Script Scripting interpreted language


























- Slides: 26

Javascript

What is Java. Script? • Scripting (interpreted) language designed for the web • Beware: Java. Script is case sensitive

Why javascript? • Supported by almost all browsers • Very popular – Lots of scripts available for download • Authoring programs such as Micromedia Flash and Director depend upon javascript • External code libraries (*. js file) – Reuseable code

History • • • Originally called Live. Script Invented by Brendan Eich at Netscape, 1995 Named changed to Java. Script because of Java Similar to Microsoft’s Jscript ECMA (European Standards Group) – Produced ECMAscript – Java. Script and JScript now based on ECMAscript

Objects • Java. Script is object oriented – Web browser Window – HTML document • Hierarchy defined by ‘dot syntax’ – Car. trunk. sparetire – Window. document. garden • Refers to the garden image in a document in a window

Instance • An incarnation of an object – car. trunk. sparetire – ford. trunk. sparetire – gm. trunk. sparetire

Properties • Instances of objects can have properties – Window. document. bg. Color

Values • Properties have values – ford. color=“green”;

Variables • Type of variable is determined by the data in it. • Var defines a global variable • my. New. Variable = 0; – Defines a local variable called my. New. Variable as a numeric value • var my. Favorite. Color = “teal”; Beetle. color = my. Favorite. Color;

Arrays • Collection of data (variable) • Can hold any type of data var spice. Rack = new Array(); spice. Rack[0] = “oregano”; spice. Rack[1] = “salt”; spice. Rack[2] = “pepper”;

Methods • An action associated with an object • Methods can take values – ford. slow. Down(“fast”); – ford. slow. Down(“slow”); • Methods can be triggered by events document. write(“Hi there!”);

Assignment Operators var my. Age = 39; my. Age += 4; // is the same as my. Age = my. Age + 4; my. Age -=4; // is the same as my. Age = my. Age – 1; my. Age *= 4; // is the same as my. Age = my. Age * 4; my. Age /= 4; // is the same as my. Age = my. Age / 4;

Comparison Operators 39 == 30 + 9 // returns true; 39 != 25 + 9 // returns true; 39 > 28 // returns true; 39 >= 39 // returns true; 39 <= 36 // returns false;

Comparison Operators (15 == 10+5) && (33 == 28 + 3); - And Logic - Returns false (15 == 10+5) || (33 = 28 + 3); - Or Logic - Returns true

Conditional Statements (if) If (Condition) { statement 1; statement 2; } Else { }

Conditional Statements (switch) switch (expression) { case label 1: //begins first case (action) statement 1; statement 2; break; case label 2: //begins second case statement 3; break; default: statement 4; }

Loop Statements (for) For (initial value of a counter; ending value; increment) { statements; }

Loop Structures (while) While (condition is true) { statements; }

Loop Structures (Do) Do { statements; } while (condition is true);

Functions • Group of Java. Script statements • Invoked by Java. Script statements function do. Something(parm 1, parm 2, etc) { var the. Visitor = parm 1; window. document. write(“Is this OK, “ + the. Visitor + “? ”); }

Events • Objects encounter events • Event Handler is the code that responds to an event

Write a Java. Script program that prints out even numbers from 0 to 10

Write a program that loads an array with the months of the year and them prints them out.

Alert • Is a method • Displays a window box with text and allows the operator to press enter. • Alert does not return anything

Prompt • Is a Method • Displays a window box designed to obtain text from the operator. • You can specify a default • Uses OK and Cancel buttons – OK returns the string that the operator entered – Cancel returns a null string

Confirm • Is a Method • Displays text in a window box designed for verification • Uses OK and Cancel – OK returns true – Cancel returns false