ABE Web Lab 21 Scheme
ABE Web Lab 21 Scheme
1. Develop and demonstrate a XHTML file that includes Javascript script for the following
problems:
a) Input: A number n obtained using prompt Output: The first n Fibonacci numbers
2. Develop and demonstrate a XHTML file that includes Javascript script for the following
problems:
Input: A number n obtained using prompt
output: A table of numbers from 1 to n and their squares using alert
3. Develop and demonstrate, using Javascript script, a XHTML document that collects the USN ( the
valid format is: A digit from 1 to 4 followed by two upper- case characters followed by two digits
followed by two upper-case characters followed by three digits; no embedded spaces allowed) of the
user. Event handler must be included for the form element that collects this information to validate
the input. Messages in the alert windows must be produced when errors are detected. Modify the
above program to get the current semester also (restricted to be a number from 1 to 8).
4. Develop and demonstrate, using Javascript script, a XHTML document that contains three short
paragraphs of text, stacked on top of each other, with only enough of each showing so that the mouse
cursor can be placed over some part of them. When the cursor is placed over the exposed part of any
paragraph, it should rise to the top to become completely visible.
5. Modify the above document so that when a paragraph is moved from the top stacking position, it
returns to its original position rather than to the bottom.
6. Design an XHTML document to store information about a student in an engineering college affiliated
to VTU. The information must include USN, Name, and Name of the College, Brach, Year of
Joining, and e-mail id. Make up sample data for 3 students. Create a CSS style sheet and use it to
display the document.
7. Write a JavaScript to design a simple calculator to perform the following operations: sum, product,
difference and quotient
8. Write a JavaScript that calculates the squares and cubes of the numbers from 0 to 10 and outputs
HTML text that displays the resulting values in an HTML table format
9. Write a JavaScript code that displays text “TEXT-GROWING” with increasing font size in the
interval of 100ms in RED COLOR, when the font size reaches 50pt it displays “TEXT-
SHRINKING” in BLUE color. Then the font size decreases to 5pt.
10. Develop and demonstrate a HTML5 file that includes JavaScript script that uses functions for the
following problems:
a. Parameter: A string
Output: The position in the string of the left-most vowel
b. Parameter: A number
Output: The number with its digits in the reverse order
EXPERIMENT NO. 1
PROGRAM:
<head>
<title>Fibonacci Series</title>
</head>
<body>
<script type="text/javascript">
var fib1=0,fib2=1,fib=0;
if(num==1)
{
document.write("<h2>" + fib1 + "</h2>");
document.write("<h2>" + fib2 + "</h2>");
}
for(i=3;i<=num; i++)
{
else
alert("Invalid Input");
</script>
</body>
</html>
SAMPLE OUTPUT
EXPERIMENT NO. 2
PROGRAM:
AIM: To display the square of a given numbers using Javascript
<head>
</head>
<body>
<script type="text/javascript">
alert(msgstr);
}
else
alert("Invalid Input");
</script>
</body>
</html>
Note:
The \u00B2 character displays superscript 2 in the javascript boxes.
Code:
msgstr = msgstr + i + "\u00B2 = " + i*i + "\n";
SAMPLE OUTPUT
EXPERIMENT NO. 3
PROGRAM:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
formValidator()
return false;
else if(!usn.value.match(usnExp))
usn.focus();
return false;
}
</script>
</head>
<body>
<br />
</form>
</body>
</html>
SAMPLE OUTPUT
EXPERIMENT No.3
<script type="text/javascript">
function formValidator()
{
var usn = document.getElementById('usnFrm');
var sem = document.getElementById('semFrm');
usnExp=/[1-4][A-Z][A-Z]\d{2}[A-Z][A-Z]\d{3}$/
semExp=/^([1-8])$/
if(usn.value.length==0)
{
alert("USN is empty.");
usn.focus();
return false;
}
else if(!usn.value.match(usnExp))
{
alert("USN should bein VTU USN format, eg., 4GK10CS001");
usn.focus();
return false;
}
else if(sem.value.length==0)
{
alert("Semester field is empty.");
sem.focus();
return false;
}
else if(!sem.value.match(semExp))
{
alert("Semester number should be from 1 to 8");
sem.focus();
return false;
}
alert("USN: "+usn.value+"\n Semester: "+sem.value);
return true;
}
</script>
</head>
<body>
<h1>Form validation using javascript</h1>
<form onSubmit = "formValidator()">
<p>Enter your VTU USN : <input type="text" id="usnFrm"/>
</p>
<p>Enter your current semester : <input type="text" id="semFrm"/>
</p>
<input type ="submit" value="SUBMIT"/>
</form>
</body>
</html>
SAMPLE OUTPUT
EXPERIMENT No.4
AIM: To display the stacking of elements
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Paragraph Stacking </title>
<style type="text/css">
.para
{
border: solid thin black;
padding:1cm;
position:absolute;
width:300px;
}
#layer1
{
background-color:yellow;
top:200px; left:400px;
z-index:1;
}
#layer2
{
background-color:red;
position:absolute;
top:220px; left:420px;
z-index:2;
}
#layer3
{
background-color:blue;
top:240px; left:440px;
z-index:3;
}
</style>
<script type="text/javascript">
var topLayer = "layer3";
function mover(toTop) {
var oldTop = document.getElementById(topLayer).style;
var newTop = document.getElementById(toTop).style;
oldTop.zIndex = 0;
newTop.zIndex = 5;
topLayer = document.getElementById(toTop).id;
}
</script>
</head>
<body>
SAMPLE OUTPUT
EXPERIMENT No.5
AIM: To display the stacking of elements
newTop.zIndex = 5;
topLayer = document.getElementById(toTop).id;
origPos=pos;
}
function moveBack()
{
var layer = document.getElementById(topLayer).style;
layer.zIndex=origPos;
}
</script>
</head>
<body>
<h3>Visibility of stacked paragraphs using Javascript</h3>
<div id="layer1" class="para" onMouseOver="mover('layer1','1')" onMouseOut="moveBack()">
10CSL78 </div>
<div id="layer2" class="para" onMouseOver="mover('layer2','2')" onMouseOut="moveBack()">
------------Web Programming Laboratory----------</div>
<div id="layer3" class="para" onMouseOver="mover('layer3','3')" onMouseOut="moveBack()">
------------Experiment 5 - Stacking of paragraphs----</div>
</body>
</html>
SAMPLE OUTPUT:
EXPERIMENT No.6
1. studentDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="student.css"?>
<VTU>
<STUDENT>
<USN>1GD11CS001</USN>
<NAME>Arun Kumar</NAME>
<COLLEGE> GOPALAN College of Engineering</COLLEGE>
<BRANCH>Computer Science and Engineering</BRANCH>
<YEAR>2011</YEAR>
<EMAILID>[email protected]</EMAILID>
</STUDENT>
<STUDENT>
<USN>1GD10ME012</USN>
<NAME>Swaroop J</NAME>
<COLLEGE> GOPALAN College of Engineering</COLLEGE>
<BRANCH>Mechanical Engineering</BRANCH>
<YEAR>2010</YEAR>
<EMAILID>[email protected]</EMAILID>
</STUDENT>
<STUDENT>
<USN>1GD12CS030</USN>
<NAME>Pradeep L</NAME>
<COLLEGE> GOPALAN College of Engineering</COLLEGE>
<BRANCH> Computer Science and Engineering </BRANCH>
<YEAR>2012</YEAR>
<EMAILID>[email protected]</EMAILID>
</STUDENT>
</VTU>
2. student.css
VTU
{
background-color: #ffFFff;
width: 100%;
}
STUDENT
{
display: block;
margin-bottom: 30pt;
margin-left: 0;
}
USN,NAME
{
color: #FF9900;
font-size: 14pt;
}
COLLEGE,BRANCH,YEAR
{
display: block;
color: #000000;
margin-left: 20pt;
}
EMAILID
{
display: block;
color: #0000FF;
margin-left: 20pt;
font-style: italic;
}
SAMPLE OUTPUT:
7. Write a JavaScript to design a simple calculator to perform the following
operations: sum, product, difference and quotient
Explanation:
document.getElementById('res').value=ans;
return;
}
if(d=='--')
{
document.getElementById('res').value*=-1;
return;
}
if(!isNaN(document.getElementById('res').value))
document.getElementById('res').value+=d;
else
document.getElementById('res').value=d;
}
Output:
Initial
view:
8. Write a JavaScript that calculates the squares and cubes of the numbers from 0 to
10 and outputs HTML text that displays the resulting values in an HTML table
format.
Explanation:
This program aims at demonstrating the usefulness of Java script in generating dynamic
web pages using Java script. Accepting user input as web page loads is also demonstrate
in this program. This script also illustrates validation using Java script.
Program
HTML File: (2.html)
<html>
<script type=text/javascript src=2.js>
</script>
<body onload=sc()>
</body>
</html>
function sc()
{
rng=prompt('Enter the range'); res=rng.split("-");
if(res.length!=2)
{
alert("invalid range "); return;
}
first=parseInt(res[0]); second=parseInt(res[1]);
if(first>second)
{
alert("invalid range ");
return;
}
str="<table border=2><tr><th>Number</th><th>Square</th><th>Cube</th></tr>";
for(i=first;i<=second;i++)
{
str=str+"<tr><td>"+i+"<td>"+(i*i)+"<td>"+(i*i*i);
}
document.write(str);
}
9. Write a JavaScript code that displays text “TEXT-GROWING” with increasing font
size in the interval of 100ms in RED COLOR, when the font size reaches 50pt
it displays “TEXT-SHRINKING” in BLUE color. Then the font size decreases to
5pt.
Explanation:
This program brings out the event handling capabilities of Java script. Timer event
handling using Java script is explained in this program. Further, the use of style
attributes in HTML and Java script is demonstrated. This program also brings out
usefulness of Java script in handling dynamic positioning of elements in HTML page.
These give the life to a web page and make it engaging.
Program:
HTML (9.html)
<html>
<script type=text/javascript src=9.js>
</script>
<body onload=fnts()>
<p id="p1" style="font-size:12pt;color=black"> TEXT-GROWING </p>
</body>
</html>
ifntsize=parseInt(fntsize);
window.setTimeout(fntGS,100,ifntsize);
function fntGS(ifs)
{
if(grow)
{
ifs=ifs+1;
if(ifs<=50
)
{
document.getElementById("p1").style.fontSize=ifs+"pt";
}
else
{
grow=false;
document.getElementById("p1").style.color="blue";
document.getElementById("p1").innerHTML="TEXT-SHRINKING";
}
else
{
ifs=ifs-1;
if(ifs<5)
return;
document.getElementById("p1").style.fontSize=ifs+"pt
";
}
window.setTimeout(fntGS,100,ifs);
}
OUPUT:
9.Write a JavaScript code that displays text “TEXT-GROWING” with increasing
font size in the interval of 100ms in RED COLOR, when the font size reaches
50pt it displays “TEXT-SHRINKING” in BLUE color. Then the font size
decreases to 5pt.
Explanation:
This program brings out the event handling capabilities of Java script. Timer event
handling using Java script is explained in this program. Further, the use of style
attributes in HTML and Java script is demonstrated. This program also brings out
usefulness of Java script in handling dynamic positioning of elements in HTML page.
These give the life to a web page and make it engaging.
Program:
HTML (9.html)
<html>
<script type=text/javascript src=3.js>
</script>
<body onload=fnts()>
<p id="p1" style="font-size:12pt;color=black"> TEXT-GROWING </p>
</body>
</html>
ifntsize=parseInt(fntsize);
window.setTimeout(fntGS,100,ifntsize);
function fntGS(ifs)
{
if(grow)
{
ifs=ifs+1;
if(ifs<=50
)
{
document.getElementById("p1").style.fontSize=ifs+"pt";
}
else
{
grow=false;
document.getElementById("p1").style.color="blue";
document.getElementById("p1").innerHTML="TEXT-SHRINKING";
}
10.Develop and demonstrate a HTML5 file that includes JavaScript script that
uses functions for the following problems:
a. Parameter: A string
Output: The position in the string of the left-most vowel
b. Parameter: A number
Output: The number with its digits in the reverse order
Explanation:
This program demonstrates the structure of HTML5 files and
semantic structure followed. Writing of functions in Java script and form
based event handling in Java script is also illustrated. The dynamic
processing of Java script without reloading HTML5 pages is also
shown.
</section>
<hr>
<section>
<h1>Reverse of a number</h1>
<p>Enter a number: <input type=text id=t2></p>
<input type=button value=Reverse
onclick=alert(reverse(document.getElementById('t2').value))>
</section>
/body>
</html>
Java script (10.js)
function findLMV(str)
{
for(i=0;i<str.length;i++)
{
if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' ||
str.charAt(i)=='o' || str.charAt(i)=='u' )
return ("Left most vowel of " + str + " is at location " + (i+1) );
}
return ("No vowels found for string "+ str);
function reverse(num)
{
rnum=0; temp=num; if(isNaN(num))
{
return "invalid number";
}
while(num!=0)
{
rnum *= 10; rnum+= num % 10; num -=
num % 10;
num = Math.floor(num/10);
}
return "Reverse of num "+ temp + " is " + rnum;
}