0% found this document useful (0 votes)
20 views

Practical No 10

The document provides 4 examples of JavaScript programs that create, read, and store cookies: 1) A program that creates a cookie containing a user's name when they enter text into an input field. 2) A program that creates a persistent cookie that expires after 2 days by setting the expiration date. 3) A program with functions to set and read a cookie containing a person's name. It displays the cookie value or a message if the cookie is not found. 4) A program that stores a user's password as a cookie value when they submit a form with their username and password entered. It executes the cookie storing function on password change.

Uploaded by

dshinde02568
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Practical No 10

The document provides 4 examples of JavaScript programs that create, read, and store cookies: 1) A program that creates a cookie containing a user's name when they enter text into an input field. 2) A program that creates a persistent cookie that expires after 2 days by setting the expiration date. 3) A program with functions to set and read a cookie containing a person's name. It displays the cookie value or a message if the cookie is not found. 4) A program that stores a user's password as a cookie value when they submit a form with their username and password entered. It executes the cookie storing function on password change.

Uploaded by

dshinde02568
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical no 10

1)Write a JavaScript program to create a cookie.


<html>
<head>
<script>
function createCookie()
{
var x = document.getElementById('myname').value
document.cookie = "name=" + x + ";"
alert("Cookie Written..")
}
</script>
</head><body>
Enter ur name <input type="text" id="myname"
onchange="createCookie()"/>
</body></html>

2) Write a program to create a persistent cookie


<html>
<body>
<script>
var date = new Date();
var days=2;
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = date.toGMTString();
document.cookie = "user=Rahul; expires="+ expires + ";"
alert("Cookie Created\n"+document.cookie)
</script>
</body>
</html>
3) Develop a program to create and read a cookie
<html>
<head>
<script>
function setCookie()
{
var name = document.getElementById('person').value
document.cookie = "name=" + name + ";"
alert("Cookie Created")
}
function readCookie()
{
var cookie = document.cookie
var panel = document.getElementById('panel')
if(cookie == "")
panel.innerHTML = "Cookie not found"
else
panel.innerHTML = cookie
}
</script>
</head>
<body>
<form name="myForm">
Enter your name <input type="text" id="person"/><br/>
<input type="button" value="Set Cookie" onclick="setCookie()"/>
<input type="button" value="Read Cookie"
onclick="readCookie()"/>
<p id="panel"></p>
</form>
</body>
</html>
4) Write a webpage that displays a form that contains an
input for Username and Password. User is prompted to
enter the input and password and password becomes value
of the cookie. Write the JavaScript function for storing the
cookie. It gets executed when the password changes
<html>
<head>
<script>
function storeCookie()
{
var pwd = document.getElementById('pwd').value
document.cookie = "Password=" + pwd + ";"
alert("Cookie Stored\n" + document.cookie)
}
</script>
</head><body>
<form name="myForm">
Enter Username <input type="text" id="uname"/><br/>
Enter Password <input type="password" id="pwd"/><br/>
<input type="button" value="Submit" onclick="storeCookie()"/>
<p id="panel"></p>
</form>
</body></html>

You might also like