Web Lab Programs
Web Lab Programs
TABLE OF CONTENTS
1 Create a form having number of elements (Textboxes, Radio buttons, Checkboxes, and so
on). Write JavaScript code to count the number of elements in a form.
2 Create a HTML form that has number of Textboxes. When the form runs in the Browser fill
the textboxes with data. Write JavaScript code that verifies that all textboxes has been filled.
If a text boxes has been left empty, popup an alert indicating which textbox has been left
empty.
1
WEB PROGRAMMING LAB MANUAL
SOLUTIONS
<script type="text/javascript">
function countFormElements()
{
alert("the number of elements are :"+document.myForm.length);
}
</script>
</head>
</body>
</html>
2
WEB PROGRAMMING LAB MANUAL
OUTPUT:
2. Create a HTML form that has number of Textboxes. When the form runs in
the Browser fill the textboxes with data. Write JavaScript code that verifies that
all textboxes has been filled. If a text boxes has been left empty, popup an alert
indicating which textbox has been left empty.
SOLUTION:
<html>
<head>
<script type="text/javascript">
function validate()
{
var myArray=new Array();
for(var i=0;i<document.myForm.length;i++)
{
if(document.myForm.elements[i].value.length==0)
{
3
WEB PROGRAMMING LAB MANUAL
myArray.push(document.myForm.elements[i].name);
}
}
if(myArray.length!=0)
{
alert("The following text boxes are empty:\n"+myArray);
}
}
</script>
</head>
<body bgcolor=pink >
<h1 align=center> Text Box Validation </h1>
OUTPUT:
4
WEB PROGRAMMING LAB MANUAL
5
WEB PROGRAMMING LAB MANUAL
6
WEB PROGRAMMING LAB MANUAL
SOLUTION:
<html>
<head>
<script type = "text/javascript">
function math_exp()
{
var x = document.form1.exptext.value;
var result = eval(x);
document.form1.resulttext.value = result;
}
</script>
</head>
<body bgcolor=pink >
<h1 align=center> Evaluating Arithmetic Expression</h1>
<form name = "form1">
</form>
</body>
</html>
7
WEB PROGRAMMING LAB MANUAL
OUTPUT:
8
WEB PROGRAMMING LAB MANUAL
4. Create a page with dynamic effects. Write the code to include layers and
basic animation.
SOLUTION:
<html>
<head>
<title> Basic Animation </title>
<style>
#layer1 {position:absolute; top:50px;left:50px;}
#layer2 {position:absolute;top:50px; left:150px;}
#layer3 {position:absolute; top:50px;left:250px;}
</style>
<script type="text/javascript">
function moveImage(layer)
{
var top=window.prompt("Enter Top value");
var left=window.prompt("Enter Left Value");
document.getElementById(layer).style.top=top+'px';
document.getElementById(layer).style.left=left+'px';
}
</script>
<head>
<body bgcolor=pink>
<div id="layer1"><img src="rose.jpg" onclick="moveImage('layer1')" alt="MyImage"></div>
</html>
9
WEB PROGRAMMING LAB MANUAL
OUTPUT:
10
WEB PROGRAMMING LAB MANUAL
11
WEB PROGRAMMING LAB MANUAL
<html>
<head>
<title>Natural Numbers</title>
<script type="text/javascript">
function sum()
var n =parseInt(num);
var sum=(n*(n+1))/2;
</script>
</head>
<body bgcolor=pink>
<hr>
<form align=center>
</form>
</body>
</html>
12
WEB PROGRAMMING LAB MANUAL
OUTPUT:
13
WEB PROGRAMMING LAB MANUAL
6. Write a JavaScript code block using arrays and generate the current date
in words, this should include the day, month and year.
SOLUTION:
<html>
<head>
<script type="text/javascript">
function display()
var currDate=dateObj.getDate();
var month=dateObj.getMonth();
var currYear=dateObj.getFullYear();
14
WEB PROGRAMMING LAB MANUAL
var days=["First","Second","Third","Fourth","Fifth","Sixth","Seventh","Eigth","Ninth",
"Tenth","Eleventh","Twelfth","Thirteenth","Fourteenth","fifteenth","Sixteenth",
"Seventeenth", "Eighteenth", "Nineteenth", "Twenty", "Twenty First", "Twenty Second",
"TwentyThird","TwentyFourth","TwentyFifth","TwentySixth","TwentySeventh","Twenty
Eight", "Twenty Nine", "Thirty", "Thirty First"];
if(currYear==2020)
else
</script>
</head>
<body bgcolor=pink>
<form>
</form>
</body>
</html>
15
WEB PROGRAMMING LAB MANUAL
OUTPUT:
16
WEB PROGRAMMING LAB MANUAL
<html>
<head>
<title>Registration Form</title>
function calc()
m1 = parseInt(document.form1.wp.value);
m2 = parseInt(document.form1.sp.value);
m3 = parseInt(document.form1.cg.value);
total = m1+m2+m3;
avg = total/3;
result = "fail";
grade = "D";
result = "Distinction";
grade = "A+";
17
WEB PROGRAMMING LAB MANUAL
grade = "A";
grade = "B";
grade = "C";
result = "Fail";
Grade = "D";
document.form1.result.value = result;
document.form1.grade.value = grade;
document.form1.total.value = total;
document.form1.average.value = avg;
18
WEB PROGRAMMING LAB MANUAL
</script>
</head>
<body bgcolor=pink>
<body>
<tr>
</tr>
<tr>
<td> Semester</td>
</tr>
<tr>
</tr>
<tr>
<td>Web Programming</td>
</tr>
<tr>
<td>Computer Graphics</td>
19
WEB PROGRAMMING LAB MANUAL
</tr>
<tr>
<td>System Programming</td>
</tr>
<tr>
<td colspan = "2" align = "center"><input type = "button" onclick = "calc()" value =
"CALCULATE" /></td>
</tr>
<tr>
<td>Total</td>
</tr>
<tr>
<td>Average</td>
</tr>
<tr>
<td>Result</td>
</tr>
<tr>
20
WEB PROGRAMMING LAB MANUAL
<td>Grade</td>
</tr>
</table>
</form>
</body>
</html>
OUTPUT:
21
WEB PROGRAMMING LAB MANUAL
22
WEB PROGRAMMING LAB MANUAL
<html>
<head>
<body bgcolor=pink>
<body>
<script type="text/javascript">
function showSalary()
var name=document.getElementById("empname").value;
var empno=document.getElementById("empno").value;
var hra=basic*0.4;
// da is 60% of basic
var da=basic*0.6
gross=basic+hra+da;
// pf is 13% of gross
var pf=gross*0.13;
23
WEB PROGRAMMING LAB MANUAL
var tax=0.2*gross;
var deductions=pf+tax;
var netsalary=gross-deductions;
document.write("<body bgcolor=skyblue>");
document.writeln("</table>");
document.write("</body>");
24
WEB PROGRAMMING LAB MANUAL
</script>
<form>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
<br>
</form>
</html>
25
WEB PROGRAMMING LAB MANUAL
OUTPUT:
26
WEB PROGRAMMING LAB MANUAL
27
WEB PROGRAMMING LAB MANUAL
9. Create a form consists of a two Multiple choice lists and one single choice
list,
The first multiple choice list, displays the Major dishes available.
The second multiple choice list, displays the Starters available.
The single choice list, displays the Soft drinks available.
The selected items from all the lists should be captured and displayed in a Text
Area along with their respective costs. On clicking the ‘Total Cost’ button, the
total cost of all the selected items is calculated and displayed at the end in the
Text Area. A ‘Clear’ button is provided to clear the Text Area.
SOLUTION:
<html>
<head>
<script text="text/javascript">
function findCost()
var major=document.getElementById("major");
var totcost=0;
28
WEB PROGRAMMING LAB MANUAL
if(option.selected==true)
totcost=totcost + price;
selectedItems=selectedItems+option.text+"\t\t"+price+"\n";
if(option.selected==true)
totcost=totcost + price;
selectedItems=selectedItems+option.text+"\t\t"+price+"\n";
var softdrinkIndex=soft.selectedIndex;
if(softdrinkIndex!=-1)
29
WEB PROGRAMMING LAB MANUAL
var selectedSoftdrink=soft.options[soft.selectedIndex].text;
totcost=totcost+price;
document.getElementById("ordereditems").value=selectedItems;
</script>
</head>
<hr>
</tr>
<tr>
30
WEB PROGRAMMING LAB MANUAL
</td>
</tr>
<tr>
</td>
</tr>
<tr>
<option value=10>LimeSoda</option>
</select>
</td>
31
WEB PROGRAMMING LAB MANUAL
</tr>
<tr>
</textarea>
</td>
</tr>
<tr>
</tr>
</table>
</form>
</html>
OUTPUT:
32
WEB PROGRAMMING LAB MANUAL
33
WEB PROGRAMMING LAB MANUAL
10. Create a web page using two image files, which switch between one another
as the mouse pointer moves over the images. Use the onMouseOver and
onMouseOut event handlers.
SOLUTION:
<html>
<head>
<style type="text/css">
</style>
<script type="text/javascript">
function changeImage()
var imageOne=document.getElementById("image1").style;
var imageTwo=document.getElementById("image2").style;
if(imageOne.visibility=="visible")
imageOne.visibility="hidden";
imageTwo.visibility="visible";
else
imageOne.visibility="visible";
imageTwo.visibility="hidden";
34
WEB PROGRAMMING LAB MANUAL
</script>
</head>
<hr>
<form >
</form>
</body>
</html>
OUTPUT:
35
WEB PROGRAMMING LAB MANUAL
36
TABLE OF CONTENTS
1
1. Write HTML Code to Illustrate formatting tags
SOLUTION
<html>
<head>
<title>TAGS</title>
</head>
<body bgcolor=pink>
<h1 align=center>FORMATTING TAGS</h1>
<p>This text is normal.</p><br>
<b>This text is bold.</b><br>
<strong>This text is strong</strong><br>
<i>This text is italic</i><br>
<em>This text is emphasized</em><br>
<h2>HTML <small>Small</small> Formatting</h2><br>
<h2>HTML <mark>Marked</mark> Formatting</h2><br>
<p>My favorite color is <del>blue</del> red.</p>
<p>My favorite <ins>color</ins> is red.</p>
<p>This is <sub>subscripted</sub> text.</p>
<p>This is <sup>superscripted</sup> text.</p>
</body >
</html>
2
OUTPUT:
3
2. Write HTML Code to Illustrate list tags.
SOLUTION:
<html>
<body>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<dl>
<dt>Coffee</dt>
<dt>Milk</dt>
</dl>
</body>
</html>
4
OUTPUT:
5
3. Write HTML code to illustrate table tags
SOLUTION:
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h2>Table tags</h2>
<p>Use the CSS border property to add a border to the table.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
6
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
OUTPUT:
7
4. Write an HTML code to design college admission Form
SOLUTION
<html>
<head>
<title>College admission Form</title>
</head>
<body>
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
Gender:<br>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br><br>
Stream: <br>
<input type="checkbox" name="stream1" value="arts"
checked="checked">Arts
<br>
<input type="checkbox" name="stream2" value="science">Science
<br>
<input type="checkbox" name="stream3"
value="commerce">Commerce
<br><br>
8
Mobile:<br>
<input type="text" name="mobile">
<br><br>
Address:<br>
<textarea name="address" rows="4" cols="50"> </textarea>
<br> <br>
Course:<br>
<select name = "course" size=2>
<option> BBA </option>
<option> BCA </option>
<option> BSc </option>
<option> Bcom</option>
<option> BA</option>
</select>
<br>
OUTPUT:
9
5. Write a program to illustrate Prompt window
<html>
<body>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var person = prompt("Please enter your name", "");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! How are you today?";
}
}
</script>
</body>
</html>
10
6. Write a Javascript to reverse a given number.
<html>
<head>
<title>Reverse a Number</title>
<script type="text/javascript">
var a,b=0;
a=parseInt(prompt("Enter number to reverse:"));
while(a>0)
{
b=b*10;
b=b+parseInt(a%10);
a=parseInt(a/10);
}
document.write("Reversed number:",b);
</script>
</head>
<body bgcolor=pink >
</body>
</html>
11
if(n==0)
return 1;
else
return n*factorial(n-1);
}
var num;
num=parseInt(window.prompt("Enter a number:"));
window.alert("Factorial of a number " + num +" is: " +factorial(num));
</script>
</head>
<body bgcolor=pink >
<h1 align=center>FACTORIAL OF NUMBER</H1>
</body>
</html>
Lab9.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="lab9.css"?>
<KLE>
<STUDENT>
<USN>BCA160001</USN>
<NAME>Arun Kumar</NAME>
12
<COLLEGE> RLS </COLLEGE>
<BRANCH>BCA</BRANCH>
<YEAR>2016</YEAR>
<EMAILID>[email protected]</EMAILID>
</STUDENT>
<STUDENT>
<USN>BBA160002</USN>
<NAME>Swaroop J</NAME>
<COLLEGE> Lingaraj</COLLEGE>
<BRANCH>BBA</BRANCH>
<YEAR>2016</YEAR>
<EMAILID>[email protected]</EMAILID>
</STUDENT>
</KLE>
lab9.css
KLE
{
background-color:white;
width: 100%;
}
STUDENT
{
display: block;
margin-bottom: 30pt;
margin-left: 0;
}
13
USN,NAME
{
color: red;
font-size: 14pt;
}
COLLEGE,BRANCH,YEAR
{
display: block;
color: blue;
margin-left: 20pt;
}
EMAILID
{
display: block;
color: maroon;
margin-left: 20pt;
font-style: italic;
}
Lab10.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<style>
body {background-color: lightblue ;}
</style>
</head>
<body>
<h2> ABOUT COLLEGE</h2>
<xsl:apply-templates select="/KLE/COLLEGE/INFO"/>
15
</body>
</html>
</xsl:template>
</xsl:stylesheet>
OUTPUT:
<head>
<script type="text/javascript">
function formValidator()
usnExp=/[a-zA-Z]{2}\d{6}/
if(usn.value.length==0)
alert("USN is empty.");
16
usn.focus();
return false;
else if(!usn.value.match(usnExp))
usn.focus();
return false;
return true;
</script>
</head>
<body>
<br />
17
</form>
</body>
</html>
OUTPUT:
18
19