Sunday, September 22, 2019

What are Cookies in JavaScript? Explain it with example.

A cookie is an amount of information that persists between a server-side and a client-side. A web browser stores this information at the time of browsing.

A cookie contains the information as a string generally in the form of a name-value pair separated by semi-colons. 

It maintains the state of a user and remembers the user's information among all the web pages.

How Cookies Works?

  • When a user sends a request to the server, then each of that request is treated as a new request sent by the different user.
  • So, to recognize the old user, we need to add the cookie with the response from the server.
  • browser at the client-side.
  • Now, whenever a user sends a request to the server, the cookie is added with that request automatically. Due to the cookie, the server recognizes the users.
JavaScript Cookies

How to create a Cookie in JavaScript?

In JavaScript, we can create, read, update and delete a cookie by using document.cookie property.

The following syntax is used to create a cookie:
  1. document.cookie="name=value";  

JavaScript Cookie Example

Example 1

Let's see an example to set and get a cookie.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. </head>  
  5. <body>  
  6. <input type="button" value="setCookie" onclick="setCookie()">  
  7. <input type="button" value="getCookie" onclick="getCookie()">  
  8.     <script>  
  9.     function setCookie()  
  10.     {  
  11.         document.cookie="username=Duke Martin";  
  12.     }  
  13.     function getCookie()  
  14.     {  
  15.         if(document.cookie.length!=0)  
  16.         {  
  17.         alert(document.cookie);  
  18.         }  
  19.         else  
  20.         {  
  21.         alert("Cookie not available");  
  22.         }  
  23.     }  
  24.     </script>  
  25.   
  26. </body>  
  27. </html>  

Example 2

Here, we display the cookie's name-value pair separately.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. </head>  
  5. <body>  
  6. <input type="button" value="setCookie" onclick="setCookie()">  
  7. <input type="button" value="getCookie" onclick="getCookie()">  
  8.     <script>  
  9.     function setCookie()  
  10.     {  
  11.         document.cookie="username=Duke Martin";  
  12.     }  
  13.     function getCookie()  
  14.     {  
  15.         if(document.cookie.length!=0)  
  16.         {  
  17.             var array=document.cookie.split("=");  
  18.         alert("Name="+array[0]+" "+"Value="+array[1]);  
  19.         }  
  20.         else  
  21.         {  
  22.         alert("Cookie not available");  
  23.         }  
  24.     }  
  25.     </script>  
  26.   
  27. </body>  
  28. </html>  

Example 3

In this example, we provide choices of color and pass the selected color value to the cookie. Now, cookie stores the last choice of a user in a browser. So, on reloading the web page, the user's last choice will be shown on the screen.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. </head>  
  5. <body>  
  6.         <select id="color" onchange="display()">  
  7.                 <option value="Select Color">Select Color</option>  
  8.                 <option value="yellow">Yellow</option>  
  9.                 <option value="green">Green</option>  
  10.                 <option value="red">Red</option>  
  11.             </select>  
  12.             <script type="text/javascript">  
  13.                 function display()  
  14.                 {  
  15.                     var value = document.getElementById("color").value;  
  16.                     if (value != "Select Color")  
  17.                     {  
  18.                         document.bgColor = value;  
  19.                         document.cookie = "color=" + value;  
  20.                     }  
  21.                 }  
  22.                 window.onload = function ()  
  23.                 {  
  24.                     if (document.cookie.length != 0)  
  25.                     {  
  26.                         var array = document.cookie.split("=");  
  27.                         document.getElementById("color").value = array[1];  
  28.                         document.bgColor = array[1];  
  29.                     }  
  30.                 }  
  31.               
  32.                   
  33.             </script>  
  34. </body>  
  35. </html>  

No comments:

Post a Comment