HSC Sop2 JS
HSC Sop2 JS
HSC Sop2 JS
Create a web page in HTML having a white background and Two Button
Objects.Write code using JavaScript such that when the mouse is placed
over the first button object without clicking, the color of the background of
the page should change after every _ seconds. There should at least be 7
different and visibly distinct background colors excluding the default color.
When the second button object is clicked, appropriate message should be
displayed in Browsers status bar.
Create another web page using JavaScript where the background color
changes automatically after every _ seconds. This event must be triggered
automatically after the page gets loaded in the browser. There should at
least be 7 different and visibly distinct background colors. When the page is
unloaded, the appropriate alert message should be displayed.
Theory:
Application of JavaScript
o Client-side validation,
o Dynamic drop-down menus,
o Displaying date and time,
o Displaying pop-up windows and dialog boxes (like an alert dialog
box, confirm dialog box and prompt dialog box),
o Displaying clocks etc.
Javascript Example:
<html>
<body>
<script type="text/javascript">
document.write("JavaScript is a simple language for javatpoint learners");
</script>
</body>
</html>
JS1.HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body id="background">
<h1>Background Color is being changed every 1 seconds</h1>
<script>
var i = 0;
function change() {
}
function click_btn() {
window.status = "Background Color is being changed every 1
seconds";
}
</script>
<input type="button" name="b1" value="over here"
onmouseover="setInterval(change, 1000)">
<input type="submit" name="b2" value="click here" onclick="click_btn()">
</body>
</html>
OUTPUT:
JS2.HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body id="background" onload="setInterval(change, 1000)">
<h1>Background Color is being changed every 1 seconds</h1>
<script>
var i =
0;
function change() {
Create JavaScript program for the following form validations. Make use of HTML5
properties to do the following validations:
1) Name, address, contact number and email are required fields of the form.
2) Address field should show the hint value which will disappear when field gets focus or key
press event.
3) Telephone number should be maximum 10 digit number only.
4) Email field should contain valid email address, @ should appear only once and not at the
beginning or at end. It must contain at least one dot(.).
5) Make use of pattern attribute for email to accept lowercase, uppercase alphabets, digits
and specified symbols.
Theory:
Data Validation:
Data validation is the process of ensuring that user input is clean, correct, and useful.
Typical validation tasks are:
• has the user filled in all required fields?
• has the user entered a valid date?
• has the user entered text in a numeric field?
Most often, the purpose of data validation is to ensure correct user input.
Validation can be defined by many different methods, and deployed in many different ways.
Server side validation is performed by a web server, after input has been sent to the
server.
Client side validation is performed by a web browser, before input is sent to a web server.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<h1>Information Form</h1>
<form name="myForm" onsubmit="return validateForm()">
<label for="txt_name">Your Name</label>
<input type="text" id="txt_name" name="txt_name" required>
<br><br>
<label for="txt_address">Address</label>
<textarea id="txt_address" name="txt_address"
onkeypress="this.placeholder=''" required></textarea>
<br><br>
<label for="telephone">Contact</label>
<input type="tel" id="telephone" name="telephone"
maxlength="10" required>
<br><br>
<label for="txt_email">Email</label>
<input type="email" id="txt_email" name="txt_email"
pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}"
required>
<br><br>
<script>
function validateForm() {
// Validate name, address, contact, and email fields
var name = document.forms["myForm"]["txt_name"].value;
var address = document.forms["myForm"]["txt_address"].value;
var contact = document.forms["myForm"]["telephone"].value;
var email = document.forms["myForm"]["txt_email"].value;
if (name == "") {
alert("Name must be filled out");
return false;
}
if (address == "") {
alert("Address must be filled out");
return false;
}
if (contact == "") {
alert("Contact number must be filled out");
return false;
}
if (email == "") {
alert("Email must be filled out");
return false;
}
return true;
}
</script>
</body>
</html>
EXPERIMENT NO 8
Create event driven JavaScript program for the following. Make use of
appropriate variables, JavaScript inbuilt string functions and control
structures.
➢ To accept string from user and count number of vowels in the given
string.
Theory:
There are some rules while declaring a JavaScript variable (also known as identifiers).
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function getVowels() {
var vowelsCount = 0;
var str = document.getElementById("t1").value;
var string = str.toString();
for (var i = 0; i <= string.length - 1; i++) {
if (string.charAt(i) == "a" || string.charAt(i) == "e" || string.charAt(i) ==
"i" || string.charAt(i) == "o" || string.charAt(i) == "u") {
vowelsCount += 1;
}
}
//document.write("Total Vowels : "+vowelsCount);
document.getElementById('demo').innerHTML = "Total Vowels :
"+vowelsCount;
return vowelsCount;
}
</script>
<tr>
<td><input type="text" id="t1"></td>
<td><input type="submit" name="submit"
onclick="getVowels()"></td>
</tr>
<p id="demo"></p>
</body>
</html>
OUTPUT:
EXPERIMENT NO 9
Create JavaScript program which computes the average marks of students. Accept
six subject marks of student from user. Calculate average marks of student which
is used to determine the corresponding grades.
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
<script type = "text/javascript">
function calc()
{
var m1,m2,m3,avg = 0,total = 0, result = "",grade = "";
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;
if( m1 < 35 || m2 < 35 || m3 < 35)
{
result = "fail";
grade = "D";
}
else if(avg >= 75)
{
result = "Distinction";
grade = "A+";
}
else if(avg >= 60 && avg < 75)
{
result = "First class";
grade = "A";
}
else if(avg >= 50 && avg < 60)
{
result = "Second class";
grade = "B";
}
else if(avg >=35 && avg < 50)
{
result = "Pass class";
grade = "C";
}
else if (avg < 35)
{
result = "Fail";
Grade = "D";
}
document.form1.result.value = result;
document.form1.grade.value = grade;
document.form1.total.value = total;
document.form1.average.value = avg;
}
</script>
</head>
<body>
<form name = "form1">
<table border = "1">
<tr>
<td> Student Name</td>
<td><input type = "text" /></td>
</tr>
<tr>
<td colspan = "2" align = "center">Subject
Marks</td>
</tr>
<tr>
<td>Web Programming</td>
<td><input type = "text" name = "wp" /></td>
</tr>
<tr>
<td>Computer Graphics</td>
<td><input type = "text" name = "cg" /></td>
</tr>
<tr>
<td>System Programming</td>
<td><input type = "text" name = "sp" /></td>
</tr>
<tr>
<td colspan = "2" align = "center"><input type =
</tr>
<tr>
<td>Average</td>
<td><input type = "text" name = "average" /></td>
</tr>
<tr>
<td>Result</td>
<td><input type = "text" name = "result" /></td>
</tr>
<tr>
<td>Grade</td>
<td><input type = "text" name = "grade"/></td>
</tr>
</table>
</form>
</body>
</html>
Output: