Unit 3
Unit 3
Unit 3
Scripting
Introduction to Java script
• JavaScript is a dynamic programming
language.
<script ...>
JavaScript code
</script>
The script tag takes two important attributes:
• language: This attribute specifies what
scripting language you are using.
• type: Indicates the scripting language in use
and its value should be set to
"text/javascript".
<script language="javascript"
type="text/javascript">
JavaScript code
</script>
First JavaScript code
<!DOCTYPE html>
<html>
<body>
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try
it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Par
agraph changed.";
}</script></body></html>
External javascript
• External file -myScript.js
function myFunction() {
document.getElementById("demo").innerHT
ML = "Paragraph changed.";
}
Html page-
<head>
<script src="myScript.js"></script></head>
JavaScript variable
Syntax:
if (expression){
Statement(s) to be executed if expression is true
}
if...else statement
The if...else statement is the next form of control
statement that allows JavaScript to execute
statements in more controlled way.
Syntax:
if (expression){
Statement(s) to be executed if expression is true
}
else{
Statement(s) to be executed if expression is false
}
SWITCH STATEMENT
Syntax:
while (expression){
Statement(s) to be executed if expression is true
}
The do...while Loop
The do...while loop is similar to the while loop
except that the condition check happens at
the end of the loop. This means that the loop
will always be executed at least once, even if
the condition is false.
do{
Statement(s) to be executed;
}
while (expression);
The for Loop
The for loop is the most compact form of looping
and includes the following three important parts:
• The loop initialization where we initialize our
counter to a starting value. The initialization
statement is executed before the loop begins.
• The test statement which will test if the given
condition is true or not. If condition is true then
code given inside the loop will be executed
otherwise loop will come out.
• The iteration statement where you can increase
or decrease your counter.
for (initialization; test condition; iteration
statement){
Statement(s) to be executed if test condition is
true
}
Comments
• /* This is a comment */
<script>
function myFunction(val) {
alert("The input value has changed. The new value is: " +
val);
}
</script>
</body>
</html>
onclick
<script>
function inform(){
alert("You have activated me by clicking the grey
button! Note that the event handler is added
within the event that it handles, in this case, the
form button event tag")
}
</script>
<form>
<input type="button" name="test" value="Click me"
onclick="inform()">
</form>
onsubmit
<html> <head>
<script type="text/javascript">
<!-- function validate()
{ all validation goes here ......... return either true
or false } //-->
</script>
</head> <body>
<form method="POST" onsubmit="return
validate()">
<input type="submit" value="Submit" />
</form> </body> </html>
onload
<html>
<head><title>Body onload example</title>
</head>
<body onload="alert('This page has finished
loading!')">
Welcome to my page
</body>
</html>
onmouseover
<html>
<head>
<script type="text/javascript">
function over() {
document.write ("Mouse Over");
}
</script>
</head>
<body>
<p>Bring your mouse inside the division to see the result:</p>
<div onmouseover="over()">
<h2> This is inside the division </h2>
</div>
</body>
</html>
onreset
<html><body>
<p>When you reset the form, a function is triggered
which alerts some text.</p>
<form onreset="myFunction()">
Enter name: <input type="text">
<input type="reset"></form>
<script>
function myFunction() {
alert("The form was reset");
}
</script>
</body></html>
Form Validation
Form validation normally used to occur at the
server, after the client had entered all the
necessary data and then pressed the Submit
button. If the data entered by a client was
incorrect or was simply missing, the server
would have to send all the data back to the
client and request that the form be
resubmitted with correct information. This
was really a lengthy process which used to put
a lot of burden on the server.
JavaScript in Form Validation
JavaScript provides a way to validate form's data
on the client's computer before sending it to
the web server. Form validation generally
performs two functions:
• Basic Validation
• Data Format Validation
• Basic Validation − First of all, the form must be
checked to make sure all the mandatory fields
are filled in. It would require just a look
through each field in the form and check for
data.
if(um.checked)
{
x++;
} if(uf.checked)
{
x++;
}
if(x==0)
{
alert('Select Male/Female');
um.focus();
return false;
}
else
{
alert('Form Succesfully Submitted');
return true;
}
OBJECTS