/    /  Javascript Cookies

Javascript Cookies

 

A javascript cookie is an amount of information that persists between a server and a client-side. A web browser stores this information(cookie) 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(;). cookies maintain the state of a user and remember the user’s information among all the web pages.

 

How Cookies Works:

 

  • When a user sends a request to the server, then each one of that request is treated as a new request sent by multiple users.
  • The browser at the client-side.

 

Javascript Cookies

 

syntax :

 

document.cookie="name=value";

 

EXAMPLE:

 

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
<input type="button" value="setCookie" onclick="setCookie()"> 
<input type="button" value="getCookie" onclick="getCookie()"> 
  <script> 
  function setCookie() 
  { 
    document.cookie="username=Duke Martin"; 
  } 
  function getCookie() 
  { 
    if(document.cookie.length!=0) 
    { 
    alert(document.cookie); 
    } 
    else 
    { 
    alert("Cookie not available"); 
    } 
  } 
  </script> 

</body> 
</html>