Cookies are used to
store data in client's computer in small text files.
Cookies store important information about the sites we visit in the browser. This information is used by the browser next time when
we visit the same website. It helps in loading the
webpage faster than the first time as some of the data is
already stored in the cookies and need not to get downloaded again to the browser.
The cookies are also used to store
login information and to hold login tokens from the websites like
Google, etc.. so we need not to login again and again.
property is used to create ,delete and read the cookies..
document.cookie = "username=John Snow";
An expiry date (in UTC time) can also be added. By default, the cookie is deleted when the browser is closed.
document.cookie = "username=John Snow; expires=Thu, 18 Dec 2013 12:00:00 UTC";
||It maintains the state of a cookie up to the specified date and time.
expires but here, time is given in seconds.
||It changes the scope of the cookie to all the pages of a website.
||It is specifies the valid domain for the cookie.
var visitor_name = prompt("What's your name?","");
var expr_date = new Date("December 30, 2050");
var cookie_date = expr_date.toUTCString();
final_cookie = "Name =" + encodeURIComponent(visitor_name) + "; expires on = " + cookie_date;
document.cookie = final_cookie;
The only way to update or modify a cookie is to create another cookie with the same name and path as an existing one. Creating a cookie with the same name but with a different path then that of an existing one will add an additional cookie.
Cookie expires attribute
expires attribute is used to give a time constraint for the cookie to sustain.
Once the declared time is passed, a cookie is deleted automatically.
In this example first enter your name and then the days you want to store cookie and refresh it again.
Next time the webpage will remember you.
var date = new Date();
document.cookie = cookieName + "=" + cookieValue + "; expires=" + date.toGMTString();
var name = cookieName + "=";
var allCookieArray = document.cookie.split(';');
for(var i=0; i<allCookieArray.length; i++)
var temp = allCookieArray[i].trim();
var user = accessCookie("testCookie");
alert("Welcome Back " + user + "!!!");
user = prompt("Please enter your name");
num = prompt("How many days you want to store your name on your computer?");
if (user!="" && user!=null)
createCookie("testCookie", user, num);
<h4>Enter Your name and for how many days you want to store cookie and refresh it again.</h4>