Google News
logo
Javascript Cookies
Cookies are variables that can be stored on a user's computer and be picked up by any other web pages in the correct domain and path. Cookies are set to expire after a certain length of time. They are limited to storing string values only.

Cookies can identify the computer being used, not the individual.
The most modern browser is expected to be able to store at least 300 cookies of four kilobytes (4 kb) per server or domain.
For each domain and path, you can store upto 20 cookies.
Cookies cannot access by any other computer except the visitor's computer that created the cookie.
A website can set and read only its own cookies (for example, Yahoo can’t read IE's cookies).

JavaScript can create, read and delete cookies using the document.cookie property, but it’s not really a pleasure to use. Every time you are forced to deal with split(), substring() and for loops. Please note that while you can treat document.cookie like a string variable, it’s actually more than that. For example, take the following script:

JavaScript : Setting Cookies


cookies.txt :

During the browsing session browser stores the cookies in memory, at the time of quitting they goto the file called cookies.txt. Different browser store cookies files in a different location in the disk.

There are six parts of a cookie : name, value, expires, path, domain, and security. The first two parts i.e. name and value are required and rest parts are optional.
document.cookie="NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN; secure";
Name=Value : The first part of the cookie string must have the name and value. Example which stores the string "FreeTimeLearning" to a cookie named "myWebsite". The JavaScript statement is :
document.cookie = "myWebsite=FreeTimeLearning";
Expires : The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser.

DD-Mon-YY HH:MM:SS GMT

Here is an example where the cookie will live upto Mon, 26 Jun 2017:00:00:00 GMT

document.cookie = "VisiterName=Ramana; expires=Mon, 26 Jun 2017:00:00:00 GMT; ";
cookieExpire = new Date();
cookieExpire.setMonth(cookieExpire.getMonth() + 9);
document.cookie = "VisitorName=Ramana; expires="+ expireDate.toGMTString() + ";";
Path : The path to the directory or web page that set the cookie. This may be blank if you want to retrieve the cookie from any directory or page. Ex: www.freetimelearning.com/javascript/javascript-cookies.php
document.cookie= "VisiterName=Ramana;expires=Mon,26Jun2017:00:00:00" + ";path=/php;";
Domain : Allow cookies domain names and sudomains of your site. For example 'freetimelearning'. It's main site is https://www.freetimelearning.com with a php site (http://php.freetimelearning.com), and java site (http://java.freetimelearning.com) etc,.
document.cookie= "VisiterName=Ramana;expires=Mon,26Jun2017:00:00:00" + ";path=/" + domain = freetimelearning.com;";
Secure : If this field contains the word "secure", then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists.
Javascript Cookies Example :
<html>
<head>
<title>Javascript Cookies</title>
<script type = "text/javascript">
 <!--
    function Cookie_name()
    {
       if( document.myform.customer.value == "" ){
          alert("Enter some value!");
          return;
       }
       cookievalue= escape(document.myform.customer.value) + ";";
       document.cookie="name=" + cookievalue;
       document.write ("Setting Cookies : " + "name=" + cookievalue );
    }
 //-->
</script>
 
</head>
 
<body>
 
<form name="myform" action="">
 Enter name : <br>
 <input type="text" name="customer"/><br><br>
 <input type="button" value="Submit" onclick="Cookie_name();"/>
</form>
 
</body>
</html>
Output :