ET22BTEC010 - Diya Patel - Practical 8 - Javascript DOM and Validation
ET22BTEC010 - Diya Patel - Practical 8 - Javascript DOM and Validation
<script>
function calculateFactorial() {
let num = document.getElementById('number').value;
let result = factorial(num);
document.getElementById('result').innerText = `Factorial of ${num} is ${result}`;
}
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
</script>
</body>
</html>
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 86
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
8. Create different variables of Array and string objects. And use all the functions related to that.
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array and String Operations</title>
</head>
<body>
<h1>JavaScript Array and String Operations</h1>
<h2>Array Operations</h2>
<div id="array-demo"></div>
<h2>String Operations</h2>
<div id="string-demo"></div>
<script>
// Array Creation
const fruits = ["Apple", "Banana", "Cherry"];
const numbers = [1, 2, 3, 4, 5];
const mixedArray = ["Text", 42, true, null];
// Array Methods
fruits.push("Orange"); // Add an element
numbers.pop(); // Remove the last element
const sortedFruits = fruits.sort(); // Sort the array
const fruitString = fruits.join(", "); // Convert array to string
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 87
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
// Display String Results
document.getElementById("string-demo").innerHTML = `
<p>Original Greeting: ${greeting}</p>
<p>Uppercase Greeting: ${upperCaseGreeting}</p>
<p>Substring of Greeting: ${substringGreeting}</p>
<p>Length of Greeting: ${greeting.length}</p>
`;
</script>
</body>
</html>
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 88
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
margin: 0;
padding: 0;
overflow-x: hidden; /* Prevent horizontal scroll */
}
.scrolling-message {
position: fixed;
top: 0;
left: 100%; /* Start off-screen to the right */
white-space: nowrap; /* Prevent line breaks */
font-size: 24px; /* Font size of the message */
animation: scroll 10s linear infinite; /* Animation for scrolling */
}
@keyframes scroll {
0% {
transform: translateX(0); /* Start position */
}
100% {
transform: translateX(-100%); /* End position */
}
}
</style>
</head>
<body>
</body>
</html>
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 89
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
Output:-
10. Write a javascript program to change the background color of web page repeatedly.
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Background Color</title>
<style>
body {
transition: background-color 0.5s; /* Smooth transition for color change */
}
</style>
</head>
<body>
<script>
// Array of colors to choose from
const colors = ["#FF5733", "#33FF57", "#3357FF", "#F1C40F", "#8E44AD", "#E74C3C", "#3498DB",
"#2ECC71"];
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 90
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
// Change background color every 2000 milliseconds (2 seconds)
setInterval(changeBackgroundColor, 2000);
</script>
</body>
</html>
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 91
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
11. Create a webpage containing one button. clicking on this button change the size of the window
& position.
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Resize and Move Window</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<script>
document.getElementById('resizeButton').onclick = function() {
// Set new width and height
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 92
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
const newWidth = 800; // Desired width in pixels
const newHeight = 600; // Desired height in pixels
</body>
</html>
Output:-
12. Put three button “OPEN NEW WINDOW”,”CLOSE CHILD WINDOW”,”CLOSE MAIN WINDOW” in
main HTML page, on click of first button, execute some JavaScript code that will open one child
window. Clicking on second button child window will close and third button main html page will
close.
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 93
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Window Control Example</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
}
button {
padding: 10px 20px;
font-size: 16px;
margin: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<script>
let childWindow; // Variable to hold the reference to the child window
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 94
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
// Function to close the child window
document.getElementById('closeChildButton').onclick = function() {
if (childWindow && !childWindow.closed) {
childWindow.close(); // Close the child window if it is open
alert("Child window closed.");
} else {
alert("No child window is open.");
}
};
</body>
</html>
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 95
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
<script>
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 96
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
// Get user agent
document.getElementById('userAgent').innerText = "User Agent: " + navigator.userAgent;
</body>
</html>
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 97
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 98
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
15. Write a javascript program to zoom out the image. use mouseover event for the same.
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Zoom Out on Mouseover</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="image-container">
<img id="zoomImage" src="earth.jpg" alt="Zoomable Image">
</div>
<script src="script.js"></script>
</body>
</html>
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 99
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
16. Create a page that includes a select object to change the background color of the current page.
The property that needs to be set is bgColor,Similar things for foreground color.
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Color Changer</title>
</head>
<body>
<h1>Background Color Changer</h1>
<script src="script.js"></script>
</body>
</html>
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 100
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
17. A document contains two forms, named form1 and form2. In the form1 is a field named
acc1(type=text) and form2 contains field select(type=text). Write a program that will copy
values of acc1 to select .
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Copy Values Between Forms</title>
</head>
<body>
<h1>Copy Values Between Forms</h1>
<form id="form1">
<label for="acc1">Account Name:</label>
<input type="text" id="acc1" name="acc1">
<button type="button" onclick="copyValue()">Copy Value to Form 2</button>
</form>
<form id="form2">
<label for="selectField">Select Account:</label>
<select id="selectField" name="selectField">
<option value="">Select...</option>
<!-- Options will be added here -->
</select>
</form>
<script src="script.js"></script>
</body>
</html>
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 101
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
Output:-
18. Create a webpage contains two textbox as userid and password and one submit button. Check
for validation for userid as mobile number and password should contains minimum 8 character,
with special symbol and one upper case letter.
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User ID and Password Validation</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.error {
color: red;
font-size: 0.9em;
}
</style>
</head>
<body>
<h1>User ID and Password Validation</h1>
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 102
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
<div>
<label for="userid">User ID (Mobile Number):</label>
<input type="text" id="userid" name="userid" required>
<div id="useridError" class="error"></div>
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<div id="passwordError" class="error"></div>
</div>
<button type="submit">Submit</button>
</form>
<script src="script.js"></script>
</body>
</html>
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 103
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
tr {
background-color: #D6EEEE;
}
.error {
color: red;
font-size: 0.9em;
}
</style>
</head>
<body>
<h1>Registration Page</h1>
<form id="registrationForm" action="/action_page.php" onsubmit="return validateForm()">
<table border="1" cellpadding="5" cellspacing="3">
<tr>
<td colspan="2" align="center" style="width:400px">
<h2>Registration Page</h2>
</td>
</tr>
<tr>
<td style="width:150px">Registration ID:</td>
<td style="width:250px">
<input type="text" id="registrationID" name="registrationID">
<div id="registrationIDError" class="error"></div>
</td>
</tr>
<tr>
<td style="width:150px">Password:</td>
<td style="width:250px">
<input type="password" id="password" name="password">
<div id="passwordError" class="error"></div>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="submit">Register</button>
</td>
</tr>
</table>
</form>
<script src="script.js"></script>
<script>
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 104
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010
function validateForm() {
// Clear previous error messages
document.getElementById('registrationIDError').textContent = '';
Output:-
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no 105