JavaScript Informatics for economists I
Introduction Programming language used in web pages. Simple and easy to use – written in HTML document. Client scripting language – script is sent with HTML document to web browser (client). JS is not Java. It has just similar syntax.
Characteristics Interpreted lang. – no compiling Object oriented – uses web browser and built-in objects Browser dependent Case sensitive Syntax is similar to C or Java language.
Limitations Works in web browser only. User can disable JS in browser. Many different JS versions. Cannot access to files or system object. Cannot save data.
Writing JS scripts Use tags. Example: document.write(“Hello world!”);
JS variables Initialized when firstly used. Case sensitive Example: var x, y; x=10; y=”Hallo”;
JS operators value assignment: = numeric operators: +, -, *, / logical operators: ==, !=,, =
Conditions Used to decide whether the expression is true or not if (expression) { commands when true; } else { commands when false; }
Cycles Used to repeat commands. while (expression) { commands; } for (initial value, condition, increment){ commands; }
Functions Frequently used part of program function name(params){ commands; return value; }
Object document Most frequently used object in JS. Contains everything on the page. document.forms document.images
Object Math Used with mathematical operations Math.abs(x) Math.exp(x) Math.pow(a,x) Math.sqrt(x) Math.PI
Events Activated when user does action onLoad – loading document onClick – cliking on object onDblClick – doubleclicking on object onMouseOver – move mouse over object onMouseOut – move mouse out of object onFocus – focusing object (activating) onKeyPress – pressing key on object onSubmit – pressing submit form button
Communication with user Print information: alert(“some text”); document.write(“some text”); Get data from user: prompt(“prompt text”,”initial value”); parseInt, parseFloat Use forms: