Coding Question ut2 css
Coding Question ut2 css
}
</script>
</body>
</html>
Q.Write a JavaScript program to create read, update and delete cookies.—4M
ANS:
<!DOCTYPE html>
<html>
<head>
<title>cookie</title>
</head>
<body>
<form name="myform">
Enter Username:<input type="text" name="uname"><br>
Enter Password:<input type="password" name="uPass"><br>
<input type="button" onclick="CreateCookie()" value="CrateCookie"><br>
<input type="button" onclick="ReadCookie()" value="ReadCookie"><br>
<input type="button" onclick="DeleteCookie()" value="DeleteCookie"><br>
<br>
Enter New Password:<input type="password" name="nPass"><br>
<input type="button" onclick="UpdateCookie()" value="UpdateCookie"><br>
<script type="text/javascript">
var x;
function CreateCookie()
{
var d=new Date();
d.setTime(d.getTime()+(7*24*60*60));
with(document.myform)
{
document.cookie="Username:"+uname.value+" Password:"+uPass.value; expires=+d.toGMTString();
x=document.cookie;
alert("Cookie Store Successfully");
}
}
function ReadCookie()
{
if(x==="")
{
alert("Cookie not Found");
}
else
{
alert(x);
}
}
function DeleteCookie()
{
var d=new Date();
d.setTime(d.getTime()-(7*24*60*60));
with(document.myform)
{
document.cookie="Username:"+uname.value+" Password:"+uPass.value; expires=+d.toGMTString();
x="";
alert("Cookie Delete Successfully");
}
}
function UpdateCookie()
{
var d=new Date();
d.setTime(d.getTime()+(7*24*60*60));
with(document.myform)
{
document.cookie="Username:"+uname.value+" Password:"+nPass.value; expires=+d.toGMTString();
x=document.cookie;
alert("Cookie Updpate Successfully");
}
}
</script>
</form>
</body>
</html>
Q. Write a JavaScript program to link banner advertisements to different URLs.-4M
ANS:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Banner Linking with JavaScript</title>
<script type="text/javascript">
var banners = [
{ image: "banner1.jpeg", url: "https://www.flipkart.com/" },
{ image: "banner2.png", url: "https://www.amazon.in/" },
{ image: "banner3.png", url: "https://www.shopsy.in/" },
{ image: "banner4.png", url: "https://www.meesho.com/" }
];
var currentIndex = 0;
function updateBanner()
{
var bannerImg = document.getElementById('bannerImage');
var bannerLink = document.getElementById('bannerLink');
bannerImg.src = banners[currentIndex].image;
bannerLink.href = banners[currentIndex].url;
currentIndex = (currentIndex + 1) % banners.length;
}
window.addEventListener('load', function()
{
updateBanner();
setInterval(updateBanner, 5000);
});
</script>
</head>
<body>
<center>
<a id="bannerLink" href="#" target="_blank">
<img id="bannerImage" src="" width="900" height="120" alt="Banner Advertisement" />
</a>
</center>
</body>
</html>
Q. Write a JavaScript program to validate email ID of the user using regular expression.-4M
ANS:
<!DOCTYPE html>
<html>
<head>
<title>Email Validation</title>
</head>
<body>
<form name="myform">
Enter Email:<input type="email" id="email"><br>
<input type="button" value="Check" onclick="ValidateEmail()">
</form>
<script type="text/javascript">
function ValidateEmail()
{
var email=document.getElementById("email").value;
var emailPattern=/^[a-z0-9]+@[a-z]+\.[a-z]{2,3}$/;
if(emailPattern.test(email))
{
alert("Corrected Email");
}
else
{
alert("Pls Provide Correct Email Like([email protected]");
}
}
</script>
</body>
</html>
Q. Write a JavaScript for the folding tree menu-4M
ANS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Folding Tree Menu</title>
<style>
/* Style for the tree menu */
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
padding: 5px 10px;
cursor: pointer;
}
.nested {
display: none;
padding-left: 20px;
}
.active {
display: block;
}
</style>
</head>
<body>
<ul>
<li onclick="toggleMenu(event)">Item 1
<ul class="nested">
<li onclick="toggleMenu(event)">Sub-item 1.1
<ul class="nested">
<li onclick="toggleMenu(event)">Sub-item 1.1.1
<ul class="nested">
<li>Sub-item 1.1.1.2</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<script>
function toggleMenu(event) {
// Prevent the click from bubbling up to the parent li
event.stopPropagation();
</body>
</html>