Javascript: Targeted At: Entry Level Trainees
Javascript: Targeted At: Entry Level Trainees
2
Icons Used
Hands on
Questions Tools Exercise
A Welcome
Try it Out Contacts
Break
3
JavaScript Session 02 Demo: Overview
Introduction:
This demo demonstrates the different ways to include
JavaScript code in HTML.
4
JavaScript Session 02 Demo: Objective
Objective:
After completing this session, you will be able to:
» List different ways to include JavaScript code in HTML
» Define Basics and User defined functions in JavaScript
5
Demo
Problem Statement:
6
Demo (Contd.)
Code:
<HTML>
<HEAD><TITLE>Credit Card Registration Form</TITLE>
<link rel="stylesheet" href="apply.css" type="text/css" media="screen“
charset="utf-8">
<script type="text/javascript" charset="utf-8" src="validate.js"
></script>
</HEAD>
<BODY>
<form action="Success.html" onsubmit=" return validate();" method="post">
<center><h2>Online Credit Card Application Form</h2></center>
<h4>* Represents Mandatory Fields</h4>
<div class='maindiv'>
<div class='leftdiv'>
<label>First Name:*</label><input type=text maxlength='15' size='15'
id='fname' tabindex="1" alt=" " class="iClass"/><br><br>
<div>
<input type="submit" value="Submit" >
</div>
</form></BODY></HTML>
Refer File Name: Code Dump_SEC_02_Demo.txt for soft copy of the code
7
Demo (Contd.)
Java script: ”validate.js”
function validate(){
var fname = document.getElementById(“fname”);
var mname= document.getElementById(“mname”);
var lname= document.getElementById(“lname”);
if(fname.value.length==0||mname.value.length==0||lname.va
lue.length==0){
alert(“Mandatory fields should not be empty“);
return false;
}
else{
document.getElementById(‘firstdiv’).innerHTML=‘’
‘’;
}
}
Refer File Name: Code Dump_SEC_02_Demo.txt for soft copy of the code
8
Demo (Contd.)
How it Works:
As the HTML page is loaded, it starts loading
from the head section.
So, the CSS and .js files in the will be loaded
first.
Then the body loads and all the HTML
elements in it will be loaded.
Once the submit button is clicked ,the
validate() function in the form will be called.
This calls the validate() function in the
validate.js file.
9
Demo (Contd.)
10
Q&A
11
JavaScript Session 02 Demo:
Source
Cognizant Project List Maintenance System
Disclaimer: Parts of the content of this course is based on the materials available from the Web sites and
books listed above. The materials that can be accessed from linked sites are not maintained by
Cognizant Academy and we are not responsible for the contents thereof. All trademarks, service marks,
and trade names in this course are the marks of the respective owner(s).
12
You have completed the
Session 02 Demo of
JavaScript.