13 IntroJavascript
13 IntroJavascript
CS380
Client Side Scripting
2
CS380
Why use client-side programming?
3
CS380
Why use client-side programming?
4
CS380
What is Javascript?
5
CS380
What is Javascript?
6
CS380
Javascript vs Java
7
CS380
Linking to a JavaScript file:
8
script
<script src="filename" type="text/javascript"></script>
HTML
CS380
Event-driven programming
9
CS380
Event-driven programming
10
Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML
element
onmouseout The user moves the mouse away from an
HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page
CS380
A JavaScript statement: alert
12
integers and real numbers are the same type (no int
vs. double)
same operators: + - * / % ++ -- = += -= *= /= %=
many operators auto-convert types: "2" * 3 is 6
CS380
Comments (same as Java)
15
// single-line comment
/* multi-line comment */
JS
CS380
Special values: null and undefined
16
CS380
Logical operators
17
CS380
if/else statement
18
if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
JS
identical structure to Java's if/else statement
JavaScript allows almost anything as a condition
CS380
Boolean type
19
CS380
for loop
20
var sum = 0;
for (var i = 0; i < 100; i++) {
sum = sum + i;
} JS
var s1 = "hello";
var s2 = "";
for (var i = 0; i < s1.length; i++) {
s2 += s1.charAt(i) + s1.charAt(i);
}
// s2 stores "hheelllloo" JS
CS380
while loops
21
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
CS380
Popup boxes
22
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS
CS380
Buttons
23
<button>Click me!</button> HTML
CS380
JavaScript functions
24
function name() {
statement ;
statement ;
...
statement ;
} JS
function myFunction() {
alert("Hello!");
alert("How are you?");
} JS
the above could be the contents of example.js
linked to our HTML page
statements placed into functions can be evaluated
function changeText() {
var span = document.getElementById("output");
var textBox = document.getElementById("textbox");
textbox.style.color = "red";
} JS
CS380
Accessing elements:
29
document.getElementById
document.getElementById returns the DOM object
for an element with a given id
CS380
Changing element style:
30
element.style
function changeText() {
//grab or initialize text here
CS380
Javascript Objects
32
CS380
output
34
CS380
Arrays
35
CS380
Array methods
36
var a = ["Stef", "Jason"]; // Stef, Jason
a.push("Brian"); // Stef, Jason, Brian
a.unshift("Kelly"); // Kelly, Stef, Jason, Brian
a.pop(); // Kelly, Stef, Jason
a.shift(); // Stef, Jason
a.sort(); // Jason, Stef
JS
array serves as many data structures: list, queue,
stack, ...
methods: concat, join, pop, push, reverse, shift,
slice, sort, splice, toString, unshift
push and pop add / remove from back
unshift and shift add / remove from front
shift and pop return the element that is removed
Math object
37
CS380
String type
38
CS380
Window
41
CS380
Window Properties
42
CS380
Window Methods
43
CS380
44
CS380
45
CS380
46
CS380
47
CS380
getElementById() method
48
CS380
Links
50
CS380
Images
51
CS380
Table…….
52
Do it yourself
change border using javascript
change background using javascript
change color using javascript
change caption using javascript
CS380