How to Set up and get a cookie using JavaScript

For work we need two functions: getting and setting a cookie

The function of setting the cookie

 Function set_cookie (name, value, expires)
 {
 If (! Expires)
 {
 Expires = new Date ();
 }
 Document.cookie = name + "=" + escape (value) + "; expires =" + expires.toGMTString () + "; path = /";
 }

Cookie function

 Function get_cookie (name) {cookie_name = name + "=";  Cookie_length = document.cookie.length;  Cookie_begin = 0;  While (cookie_begin <cookie_length) {value_begin = cookie_begin + cookie_name.length;  If (document.cookie.substring (cookie_begin, value_begin) == cookie_name) {var value_end = document.cookie.indexOf (";", value_begin);  If (value_end == -1) {value_end = cookie_length;  } Return unescape (document.cookie.substring (value_begin, value_end));  } Cookie_begin = document.cookie.indexOf ("", cookie_begin) + 1;  If (cookie_begin == 0) {break;  }} Return null;  } 

Example of installation and reception

In the example, clicking on the "Installation" link is written to the browser cookie. We do this with the help of this function:

 Function save_cookie () {
  Var name = "example" // name of the cookie
  Var tmp = "Hi, I'm your cookie!";  // write value
  Expires = new Date ();  // get the current date
  Expires.setTime (expires.getTime () + (1000 * 86400 * 365));  // calculate the cookie expiration time
  Set_cookie (name, tmp, expires);  // set the cookie using the set_cookie function
 }

Call this function in html:

<a href="javascript:save_cookie();">Установка</a>

To read the cookie, use the function get_cookie () and for clarity, write the result to the html layer. We do this with the help of this function:

 Function write_cookie () {
  // get a cookie and write the value to the layer layer using innerHTML
  Document.getElementById ('layer'). InnerHTML = get_cookie ('example'); 
 }

Call this function in html:

<a href="javascript:write_cookie();">Чтение</a>
// слой, в котором показываем значение cookie
<div id="layer" style="padding:5px;"></div>

Example of saving and getting an array

The installation and receipt of cookies are similar to those discussed in the previous example, with the only difference that before reading and saving, you need to process the array.

We get the values ​​and write them into the cookie using the save_array () function:

 Function save_array () {
  Var myArray = [1, 2, "hello"];  // array with values
  Var tmp = "";
  If (myArray! = Null) {
 
  // we go through the array, collecting values ​​in a variable and separating them with a comma
  For (i in myArray) 
  {
  If (myArray [i]! = "") {
  Tmp = tmp + myArray [i];
  If (i! = MyArray.length-1) {
  Tmp = tmp + ",";
  }
  }
  }
  }
  Expires = new Date ();  // get the current date
  Expires.setTime (expires.getTime () + (1000 * 86400 * 365));  // calculate the cookie expiration time
  Set_cookie ("example", tmp, expires);  // set the cookie using the set_cookie function
 }

Call this function in html:

<a href="javascript:save_array();">Установка</a>

We read the value of the cookie and output it to the html layer

 Function write_array () {
  Var getmyArray = get_cookie ('example');  // read cookie value
  Var tmp = '';
  GetmyArray = getmyArray.split (",") // parse the value by placing it in an array
  For (i = 0; i <getmyArray.length; i ++) // go through the array and format it for output
  {
  Tmp + = getmyArray [i];
  If (i! = GetmyArray.length) {
  Tmp + = "<br / 7>";
  }
 }
  // output the array to the layer with the identifier layer2
  Document.getElementById ('layer2'). InnerHTML = tmp; 
 }

Call this function in html:

<a href="javascript:write_array();">Распечатать массив</a>
// слой, в котором показываем значение cookie
<div id="layer2" style="padding:5px;"></div>