Professional Documents
Culture Documents
Chapter 4
What is Java script?
2
integers and real numbers are the same type (no int
vs. double)
same operators: + - * / % ++ -- = += -= *= /= %=
similar precedence to Java
many operators auto-convert types: "2" * 3 is 6
Comments (same as Java)
7
// single-line comment
/* multi-line comment */
JS
Java/JS/PHP: // comment
PHP: # comment
Special values: null and undefined
8
"5.0" == 5 is true
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
Boolean type
11
function name() {
statement ;
statement ;
...
statement ;
} JS
function myFunction() {
alert("How are you?");
} JS
var sum = 0;
for (var i = 0; i < 100; i++) {
sum = sum + i;
} JS
var s1 = "hello";
var s2 = "";
for (var i = 0; i < s.length; i++) {
s2 += s1.charAt(i) + s1.charAt(i);
}
// s2 stores "hheelllloo" JS
while loops (same as Java)
17
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS
HTML DOM
19
Root element
<HTML>
Element
Element <head> <body>
Element :<a
>
Element <title>
Attribute:”href”
Text:”My link”
Text: “My title”
HTML DOM
20
Change
Add or
Delete HTML elements
document.getElementsByTagName(name)
document.getElementsByClassName(name)
Example
22
<html>
<head>
<title>Change attribute value</title>
</head>
<body>
<img id="myImage" src="xxx.jpg" width="200" height="200">
<script>
function change(){
document.getElementById("myImage").src="bb.jpg";
}
</script>
<form method="get">
<input type="button" onclick="change()" value="click">
</form>
</body>
</html> JS
HTML DOM Property
23
element.setAttribute(attribute,value)
element.style.property=new style
Example
24
<html> <head>
<script type="text/javascript">
function sayHello(){
document.getElementById("a1").innerHTML="Heading four";
}
</script>
</head>
<body>
<h3>Click the following button to call the function</h3>
<form method="Post">
<input type="button" onclick="sayHello()" value="Click">
</form>
<h4 id="a1">use different text in write and try</h4>
</body>
</html> JS
HTML DOM ….
25
document.removeChild(element)
document.replaceChild(element)
document.write(text)
Example
26
HTML4 FORM SHOULD BE
VALIDATED WITH
JAVA SCRIPT
27
A real Form that uses validation
28
What is form validation?
29
<html><head>
<title>Validation</title>
<script>
function validate(){
var n=document.getElementById("name").value;
var a=document.getElementById("age").value;
if(n=="" || !isNaN(n))
alert("Enter valid name");
else if(a=="" || isNaN(a))
alert("Enter valid age"); }
</script>
</head><body>
<form method="post" action="“ >
Name:<br><input type="text" id="name"><br>
Age:<br><input type="text" id="age"><br>
<input type="button" value="Submit" onclick="validate()">
</form></body></html> JS
Advantages
31
Alternate syntax:
Concatenation
Example:
String consisting entirely of four characters:
Digit followed by
A . followed by
A single space followed by
Any “word” character
Union
E.g:
Union of set of strings represented by regular
expressions
Set of single-character strings that are either a digit or a
space character
Character class: shorthand for union of one or more
ranges of characters
E.g: set of lower case letters
E.g: the \w escape code class
JavaScript Regular Expressions
46
Unions of concatenations
Kleene star
E.g: any number of digits (including none)
E.g:
Strings consisting of only “word” characters
String must contain both a digit and a letter (in either order)
Example code
48
THANKS!
YOU CAN DIG MORE …
49