JavaScript Tutorial

adplus-dvertising
Cookies in JavaScript
Previous Home Next

A cookie is a small piece of information stored as a text file on your computer, its allow you to store information on the computer of the visitor browsing your web site. Cookies are created when you use your browser to visit a website that uses cookies to keep track of your movements within the site,

A cookie can only hold string based name/value pairs (eg name=value settings ) You cannot store binary data in a cookie and cookies can be a maximum of 4kb in size each .

A cookie can consisting of four parameters:-

cookie name : its show the name of the cookie. it is the only cookie parameter that is required.

expiration date : Signifies when the cookie should expire. When this date is reached, the cookie will be automatically deleted by the web browser.

path : The URL of the page that sets the cookie.

domain : The domain that sets the cookie.

Syntax

document.
cookie="Cookiename=cookieValue;
expires=expirationDate;path=URL; domain=theDomain;"

Example :

<html>
<head>
<script>
function writeCookie( )
{
document.cookie = "yourName=" + name; // Create the cookie
}
</script>
</head>
<body>
<form action="" name="orderForm">
Enter your name: <input type="TEXT" name="nameField"/>
<input type="BUTTON" value="Write Cookie" onClick="writeCookie()" />
</form>
</body>
</html>

Output :

Previous Home Next