Css
Css
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function addToArray() {
var fruit = prompt("Please enter a fruit name", "Grapes");
if (fruit != null) {
fruits.push(fruit);
document.getElementById("demo").innerHTML = fruit + " added to the
array.";
}
}
function removeFromArray() {
fruits.pop();
document.getElementById("demo").innerHTML = "Last fruit removed from
the array.";
}
function displayArray() {
document.getElementById("demo").innerHTML = fruits.toString();
}
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Hello, World!";
function displayLength() {
document.getElementById("demo").innerHTML = "Length of string is: " +
str.length;
}
function convertToUpper() {
document.getElementById("demo").innerHTML = "String in uppercase: " +
str.toUpperCase();
}
function convertToLower() {
document.getElementById("demo").innerHTML = "String in lowercase: " +
str.toLowerCase();
}
</script>
</body>
</html>
This HTML file contains JavaScript functions handling a string "Hello,
World!" displayed in a paragraph. Three buttons trigger different string
manipulations: displaying the string length, converting the string to uppercase,
and converting it to lowercase. JavaScript functions tied to these buttons use
`getElementById` to update the HTML, showcasing the length or modified
versions (uppercase/lowercase) of the string in the designated paragraph (`<p>`).
3. Develop a Simple Javascript with Html to Create a webpage using form
elements.
<!DOCTYPE html>
<html>
<head>
<title>Simple Form</title>
</head>
<body>
<form id="myForm">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<input type="submit" value="Submit">
</form>
<script>
document.getElementById('myForm').addEventListener('submit',
function(event) {
event.preventDefault();
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
alert('Name: ' + name + ', Email: ' + email);
});
</script>
</body>
</html>
This HTML file creates a simple form with fields for "Name" and "Email" and
a submit button. JavaScript attached to the form prevents its default
submission behavior upon submission. It retrieves the values entered into the
"Name" and "Email" fields, then displays an alert with the submitted Name
and Email details.
<script>
document.getElementById('myForm').addEventListener('submit',
function(event) {
event.preventDefault();
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
alert('Name: ' + name + ', Email: ' + email);
});
</script>
</body>
</html>
This HTML file creates a simple form with fields for "Name" and "Email" and
a submit button. JavaScript attached to the form prevents its default
submission behavior upon submission. It retrieves the values entered into the
"Name" and "Email" fields, then displays an alert with the submitted Name
and Email details.
This HTML document features a form triggering various events. Upon page
load, an alert indicates the loaded page. The "Name" input field prompts an
alert when focused. The submit button triggers an alert on click and hover.
JavaScript code prevents the form's default submission behavior and displays
an alert containing the entered "Name" and "Email" values upon submission.
<script>
document.getElementById('myForm').addEventListener('submit',
function(event) {
event.preventDefault();
var email = document.getElementById('email').value;
var phone = document.getElementById('phone').value;
if (!emailRegex.test(email)) {
alert('Invalid email');
return;
}
if (!phoneRegex.test(phone)) {
alert('Invalid phone number');
return;
}
alert('Form is valid');
});
</script>
</body>
</html>
This HTML file presents a form with "Email" and "Phone" input fields. The
associated JavaScript uses an event listener to intercept form submission,
preventing default behavior. It validates the entered email using a regular
expression and displays an alert for an invalid email format. Similarly, it
verifies the phone number format using a regex and alerts for an invalid phone
number. Upon successful validation, an alert confirms the form's validity,
without submission, to the user.
In this webpage:
A status bar is created at the bottom of the page using a div element with a fixed position.
The oncontextmenu event handler is used to disable the right-click context menu for web page
protection.
The onkeydown event handler is used to disable certain keyboard shortcuts (Ctrl+C, Ctrl+V,
Ctrl+U) for web page protection.
Please note that this is a basic example and doesn’t include any advanced features. You
might want to add those in a real-world scenario. Also, keep in mind that client-side
protection methods like these can easily be bypassed by a determined user, so they should
not be relied upon for serious security measures. For serious security measures, you should
use server-side protection methods.
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Slideshow Banner</title>
</head>
<body>
<div id="slideshow">
<a href="https://www.google.com">
</a>
let currentImage = 0;
setInterval(() => {
images[currentImage].style.display = 'none';
images[currentImage].style.display = 'block';
}, 3000);
</script>
</body>
</html>
Please replace img1.jpg, img2.jpg, and img3.jpg with the paths to your actual images. This
code will display each image for 2 seconds before moving on to the next one, creating a
slideshow effect. The images are hidden by default (display:none) and only the current
image in the slideshow is displayed (display:block). The carousel function is called every 2
seconds using setTimeout. If the end of the slideshow is reached, it loops back to the first
image.
Remember to run this code in a web server environment to avoid any cross-origin issues. If
you’re running it locally, you can use server setups like Apache, Nginx, or simple python
server, etc. Also, ensure that the path to the images is correct. If the images are in the same
directory as your HTML file, you can just put the image file name (e.g., image.jpg). If
they’re in a subdirectory, include that in the path (e.g., images/image.jpg).
<script>
function openChildWindow() {
var childWindow = window.open("", "ChildWindow", "width=200,height=100");
childWindow.document.write("<h1>This is a child window</h1>");
}
</script>
</body>
</html>
In this code, clicking the button calls the openChildWindow function. This function opens a
new window with window.open and then writes some text in it with document.write.
Please note that modern web browsers block pop-ups (new windows) by default, so you may
need to disable your browser’s pop-up blocker to see the new window. Also, this code might
not work properly when run locally due to browser security restrictions. It’s recommended to
run this code on a web server. If you’re running it locally, you can use server setups like
Apache, Nginx, or simple python server, etc.
#image2 {
display: none;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<img id="image1" src="C:\Users\Neha\Pictures\Screenshots\Screenshot
2023-11-05 134302.png" alt="Image 1" onmouseover="showImage2()"
onmouseout="showImage1()">
<img id="image2" src="C:\Users\Neha\Pictures\Screenshots\Screenshot
2023-11-05 131906.png" alt="Image 2">
<script>
function showImage2() {
document.getElementById('image1').style.display = 'none';
document.getElementById('image2').style.display = 'block';
}
function showImage1() {
document.getElementById('image1').style.display = 'block';
document.getElementById('image2').style.display = 'none';
}
</script>
</body>
</html>
This code creates a webpage with two images. When the user hovers over the first image,
the second image will be displayed. When the user rolls off of the first image, the first image
will be displayed again.
The showImage2() and showImage1() functions are used to show and hide the images.
The showImage2() function is called when the user hovers over the first image, and the
showImage1() function is called when the user rolls off of the first image.
<!DOCTYPE html>
<html>
<head>
<title>Simple Menu</title>
<style>
.menu {
background-color: #f0f0f0;
overflow: hidden;
}
.menu a {
float: left;
display: block;
color: #333;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.menu a:hover {
background-color: #ddd;
color: black;
}
</style>
</head>
<body>
<div class="menu">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
<h1 id="home">Home</h1>
<p>This is the Home section.</p>
<h1 id="about">About</a></h1>
<p>This is the About section.</p>
<h1 id="contact">Contact</h1>
<p>This is the Contact section.</p>
</body>
</html>