Lab 05
Lab 05
Lab 05
What is JavaScript?
JavaScript was designed to add interactivity to HTML pages
JavaScript is a scripting language
A scripting language is a lightweight programming language
JavaScript is usually embedded directly into HTML pages
JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
Everyone can use JavaScript without purchasing a license
</script>
JavaScript Statements
A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do.
This JavaScript statement tells the browser to write "Hello " to the web page:
document.write("Hello ");
It is normal to add a semicolon at the end of each executable statement. Most people think this is a good
programming practice, and most often you will see this in JavaScript examples on the web.
The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end
of the line as the end of the statement. Because of this you will often see examples without the semicolon at the
end.
JavaScript Code
JavaScript code (or just JavaScript) is a sequence of JavaScript statements.
Each statement is executed by the browser in the sequence they are written.
Example:
<html>
<body>
<script type="text/javascript">
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
</script>
</body>
</html>
JavaScript Variables
Example:
<html>
<body>
<script type="text/javascript">
var firstname;
firstname="Hege";
document.write(firstname);
document.write("<br />");
firstname="Tove";
document.write(firstname);
</script>
<p>The script above declares a variable, assigns a value to it, displays the value, changes the value, and
displays the value again.</p>
</body>
</html>
Event Handler
1. Alert:
<form>
<input type="button" onclick=
"alert('Are you sure you want to give us the deed to your house?')"
value="Confirmation Alert">
</form>
2. Confirm
<html>
<head>
<script type="text/javascript">
<!--
function confirmation() {
if (answer){
alert("Bye bye!")
window.location = "http://www.google.com/";
else{
//-->
</script>
</head>
<body>
<form>
</body>
</html>
3. Pop Up:
<head>
<script type="text/javascript">
<!--
function myPopup() {
window.open( "http://www.google.com/" )
//-->
</script>
</head>
<body>
<form>
</form>
</body>
Summery Example:
<html>
<head>
<script type="text/javascript">
function displayDate()
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body>
</body>
</html>
Calling a function from within another function in JavaScript
<script type="text/javascript">
var numCorrect = 0;
function takeTest()
{ var response = "";
var points = 0;
response = prompt(q1,"");
if (response) points= runningTotal((response.toUpperCase() == a1) ? 1 : 0);
alert(points);
response = prompt(q2,"");
if(response) points= runningTotal((response == a2) ? 1 : 0);
alert(points);
response = prompt(q3,"");
if (response) points=runningTotal((response.toUpperCase() == a3) ? 1 : 0);
alert("You answered a total of " + points + " correctly.");
numCorrect = 0;
points = 0;
function runningTotal(i)
{ numCorrect += i;
return numCorrect;
}
Using a switch statement
<html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lab 4-6</title>
alert(OptionsSelect);
}
</script>
<body>
<h3>CIW JavaScript Specialist</h3>
<form name = "frmOne">
</form>
</body>
</html>
<head>
<script language="JavaScript" type="text/javascript">
function switchTest(SetOption) {
OptionsSelect = document.frmOne.SetOption.value
alert(OptionsSelect);
switch (OptionsSelect) {
case "1":
alert("Arizona") break
case "2":
alert("New Mexico")
break
case "3":
alert("California")
break
default:
alert("Cannot be determined")
}
}
</script>
</head>
<body>
<h3>CIW JavaScript Specialist</h3>
<form name = "frmOne">
<script type="text/javascript">
function showUpper(checked) {
var showThis;
if (checked) {
showThis = document.myForm.name.value.toUpperCase();
} else {
showThis = document. myForm.name.value.toLowerCase();
}
document.myForm.name.value = showThis;
}
function emailTest(form) {
if (form.email_address.value.indexOf("@", 0) < 0) {
alert("This is not a valid e-mail address!");
} else {
alert("This could be a valid e-mail address");
}
}
function showFirst2(form) {
var first2Chars;
first2Chars = form.phone_number.value.substring(0, 2);
alert(first2Chars);
}
</script>
<script type="text/javascript">
<!--
var myYear =
today.getFullYear(); var
myDate = today.getDate();
// -->
</script>
Lab Task 2:
1. Write a JavaScript program to swap the value of two variables without using a
third variable. Take the value of both variables from the user.
2. Write a JavaScript program to calculate the area of a square
3. Write a Javascript program to convert Kilometers to centimetrs
4. Develop a JavaScript program to perform following arithmetic operations: addition,
subtraction, multiplication and division.
5. Write a code in JavaScript to use if-else, nested if and loops.
Lab Task 3:
1. Name (Name should contains alphabets and the length should not be less than 6 characters).
2. Password (Password should not be less than 6 characters length).
3. E-mail id (should not contain any invalid and must follow the standard pattern
([email protected])
4. Phone number (Phone number should contain 10 digits only).