How to Save and Get Values From Cookies Using JavaScript

Introduction

 
In this article, we will tell how to save and get cookies using JavaScript.
 
Procedure to save a value in cookies using JavaScript
 
In the following function we pass three parameters:
  1. c_name: the name of the cookie (that you want to create)
  2. value: the value you want to save in the cookie
  3. expiredays: expiry days of your cookie
  1. function setCookie(c_name, value, expiredays) {  
  2.     var exdate = new Date();  
  3.     exdate.setDate(exdate.getDate() + expiredays);  
  4.     document.cookie = c_name + "=" + value + ";path=/" + ((expiredays ==null) ? "" : ";expires=" + exdate.toGMTString());  
Procedure to get the value from a cookie
 
In the following function we pass one parameter:
  • Name- Name of the cookie that you want to get the value of.
  1. function getCookie(name) {  
  2.     var dc = document.cookie;  
  3.     var prefix = name +"=";  
  4.     var begin = dc.indexOf("; " + prefix);  
  5.     if (begin == -1) {  
  6.         begin = dc.indexOf(prefix);  
  7.         if (begin != 0)return null;  
  8.     } else {  
  9.         begin += 2;  
  10.     }  
  11.     var end = document.cookie.indexOf(";", begin);  
  12.     if (end == -1) {  
  13.         end = dc.length;  
  14.     }  
  15.     return unescape(dc.substring(begin + prefix.length, end));  
Example
 
This is the simple live example of how to use a cookie in your application:
  1. <scriptlanguage="javascript"type="text/javascript">  
  2.             function setCookie(c_name, value, expiredays) {  
  3.                var exdate = new Date();  
  4.                 exdate.setDate(exdate.getDate() + expiredays);  
  5.                 document.cookie = c_name + "=" + value + ";path=/" + ((expiredays ==null) ? "" : ";expires=" + exdate.toGMTString());  
  6.             }  
  7.             function getCookie(name) {  
  8.                 var dc = document.cookie;  
  9.                 var prefix = name +"=";  
  10.                 var begin = dc.indexOf("; " + prefix);  
  11.                 if (begin == -1) {  
  12.                     begin = dc.indexOf(prefix);  
  13.                     if (begin != 0)return null;  
  14.                 } else {  
  15.                     begin += 2;  
  16.                 }  
  17.                 var end = document.cookie.indexOf(";", begin);  
  18.                 if (end == -1) {  
  19.                     end = dc.length;  
  20.                 }  
  21.                 return unescape(dc.substring(begin + prefix.length, end));  
  22.             }  
  23.             function savename() {  
  24.                 setCookie("SetName", document.getElementById("textName").value, 1);  
  25.             }  
  26.             function getName() {  
  27.                 var strVal = getCookie("SetName");  
  28.                 var name = document.getElementById("textName").value;  
  29.                 if (strVal == name) {  
  30.                     alert("hi i am " + name);  
  31.                 }  
  32.             }  
  33.         </script>  
  34. <formid="form1"runat="server">  
  35.          <inputtype="text"id="textName"onblur="savename();"/>  
  36.          <inputtype="button"onclick="getName();"value="Click"/>  
  37.         </form> 
example2.png