How To Manage Cookies In Javascript

By admin ·
1

document.cookie

First you should understand what the cookie is and how it’s set. A cookie is a string that is stored on the user’s computer to allow data to persist throughout the user’s session. Cookies are often set by HTTP response headers, but they can also be set directly in the browser using JavaScript. To read the entire cookie string you can access document.cookie.

document.cookie

What you’ll likely see is a long string of semi-colon delineated key/value pairs.

ps=31;_ssd3=P0-1415818163-1436218393727;favorite_animal=goat;_gaa=23.00334.....
2

Set a cookie

You can manually set a cookie by setting document.cookie equal to the desired cookie using this format:

document.cookie = "name=tyler;expires=Thu, 03 Sep 2015 05:33:44 GMT";

This can be a little cumbersome, so the function below should make things easier.

function setCookie(name, value, exdays) { var d, expires; exdays = exdays || 1; d = new Date(); d.setTime(d.getTime() + (exdays2460601000)); expires = "expires=" + d.toUTCString(); document.cookie = name + "=" + value + "; " + expires; }

To set a cookie that lasts for one year, you could use:

setCookie('name', 'Batman', 365);
3

Read a cookie

To read a cookie you can simply access document.cookie.

console.log(document.cookie);

If you’re searching for a specific value you can use the following function.

function getCookie(name) { var cookie, c; cookies = document.cookie.split(';'); for (var i=0; i < cookies.length; i++) { c = cookies[i].split('='); if (c[0] == name) { return c[1]; } } return ""; }

This function uses

JavaScript’s string split method

to split the cookie string by semi-colons. Then it loops through the result array of strings and splits them again by “=” producing the cookie key/value pair. Then it simply tests the cookie key (c[0]) against the name of the cookie you are trying to read. If it matches it returns the value (c[1]).

4

Delete a cookie

To delete a cookie, you simply need to expire it. So deleting a cookie is basically setting the cookie with an expiration in the past. Using our setCookie function:

setCookie('name', '', -1);
5

Use the jQuery cookie plugin

If you’re using jQuery, you might as well use this cookie plugin. It allows easy management of cookies.

// read $.cookie('name'); // => "Batman" // set $.cookie('name', 'Iron Man'); // delete $.removeCookie('name');
6

document.cookie

First you should understand what the cookie is and how it’s set. A cookie is a string that is stored on the user’s computer to allow data to persist throughout the user’s session. Cookies are often set by HTTP response headers, but they can also be set directly in the browser using JavaScript. To read the entire cookie string you can access document.cookie.

document.cookie

What you’ll likely see is a long string of semi-colon delineated key/value pairs.

ps=31;_ssd3=P0-1415818163-1436218393727;favorite_animal=goat;_gaa=23.00334.....
7

Set a cookie

You can manually set a cookie by setting document.cookie equal to the desired cookie using this format:

document.cookie = "name=tyler;expires=Thu, 03 Sep 2015 05:33:44 GMT";

This can be a little cumbersome, so the function below should make things easier.

function setCookie(name, value, exdays) { var d, expires; exdays = exdays || 1; d = new Date(); d.setTime(d.getTime() + (exdays2460601000)); expires = "expires=" + d.toUTCString(); document.cookie = name + "=" + value + "; " + expires; }

To set a cookie that lasts for one year, you could use:

setCookie('name', 'Batman', 365);
8

Read a cookie

To read a cookie you can simply access document.cookie.

console.log(document.cookie);

If you’re searching for a specific value you can use the following function.

function getCookie(name) { var cookie, c; cookies = document.cookie.split(';'); for (var i=0; i < cookies.length; i++) { c = cookies[i].split('='); if (c[0] == name) { return c[1]; } } return ""; }

This function uses

JavaScript’s string split method

to split the cookie string by semi-colons. Then it loops through the result array of strings and splits them again by “=” producing the cookie key/value pair. Then it simply tests the cookie key (c[0]) against the name of the cookie you are trying to read. If it matches it returns the value (c[1]).

9

Delete a cookie

To delete a cookie, you simply need to expire it. So deleting a cookie is basically setting the cookie with an expiration in the past. Using our setCookie function:

setCookie('name', '', -1);
10

Use the jQuery cookie plugin

If you’re using jQuery, you might as well use this cookie plugin. It allows easy management of cookies.

// read $.cookie('name'); // => "Batman" // set $.cookie('name', 'Iron Man'); // delete $.removeCookie('name');
11

document.cookie

First you should understand what the cookie is and how it’s set. A cookie is a string that is stored on the user’s computer to allow data to persist throughout the user’s session. Cookies are often set by HTTP response headers, but they can also be set directly in the browser using JavaScript. To read the entire cookie string you can access document.cookie.

document.cookie

What you’ll likely see is a long string of semi-colon delineated key/value pairs.

ps=31;_ssd3=P0-1415818163-1436218393727;favorite_animal=goat;_gaa=23.00334.....
12

Set a cookie

You can manually set a cookie by setting document.cookie equal to the desired cookie using this format:

document.cookie = "name=tyler;expires=Thu, 03 Sep 2015 05:33:44 GMT";

This can be a little cumbersome, so the function below should make things easier.

function setCookie(name, value, exdays) { var d, expires; exdays = exdays || 1; d = new Date(); d.setTime(d.getTime() + (exdays2460601000)); expires = "expires=" + d.toUTCString(); document.cookie = name + "=" + value + "; " + expires; }

To set a cookie that lasts for one year, you could use:

setCookie('name', 'Batman', 365);
13

Read a cookie

To read a cookie you can simply access document.cookie.

console.log(document.cookie);

If you’re searching for a specific value you can use the following function.

function getCookie(name) { var cookie, c; cookies = document.cookie.split(';'); for (var i=0; i < cookies.length; i++) { c = cookies[i].split('='); if (c[0] == name) { return c[1]; } } return ""; }

This function uses

JavaScript’s string split method

to split the cookie string by semi-colons. Then it loops through the result array of strings and splits them again by “=” producing the cookie key/value pair. Then it simply tests the cookie key (c[0]) against the name of the cookie you are trying to read. If it matches it returns the value (c[1]).

14

Delete a cookie

To delete a cookie, you simply need to expire it. So deleting a cookie is basically setting the cookie with an expiration in the past. Using our setCookie function:

setCookie('name', '', -1);
15

Use the jQuery cookie plugin

If you’re using jQuery, you might as well use this cookie plugin. It allows easy management of cookies.

// read $.cookie('name'); // => "Batman" // set $.cookie('name', 'Iron Man'); // delete $.removeCookie('name');
16

document.cookie

First you should understand what the cookie is and how it’s set. A cookie is a string that is stored on the user’s computer to allow data to persist throughout the user’s session. Cookies are often set by HTTP response headers, but they can also be set directly in the browser using JavaScript. To read the entire cookie string you can access document.cookie.

document.cookie

What you’ll likely see is a long string of semi-colon delineated key/value pairs.

ps=31;_ssd3=P0-1415818163-1436218393727;favorite_animal=goat;_gaa=23.00334.....
17

Set a cookie

You can manually set a cookie by setting document.cookie equal to the desired cookie using this format:

document.cookie = "name=tyler;expires=Thu, 03 Sep 2015 05:33:44 GMT";

This can be a little cumbersome, so the function below should make things easier.

function setCookie(name, value, exdays) { var d, expires; exdays = exdays || 1; d = new Date(); d.setTime(d.getTime() + (exdays2460601000)); expires = "expires=" + d.toUTCString(); document.cookie = name + "=" + value + "; " + expires; }

To set a cookie that lasts for one year, you could use:

setCookie('name', 'Batman', 365);
18

Read a cookie

To read a cookie you can simply access document.cookie.

console.log(document.cookie);

If you’re searching for a specific value you can use the following function.

function getCookie(name) { var cookie, c; cookies = document.cookie.split(';'); for (var i=0; i < cookies.length; i++) { c = cookies[i].split('='); if (c[0] == name) { return c[1]; } } return ""; }

This function uses

JavaScript’s string split method

to split the cookie string by semi-colons. Then it loops through the result array of strings and splits them again by “=” producing the cookie key/value pair. Then it simply tests the cookie key (c[0]) against the name of the cookie you are trying to read. If it matches it returns the value (c[1]).

19

Delete a cookie

To delete a cookie, you simply need to expire it. So deleting a cookie is basically setting the cookie with an expiration in the past. Using our setCookie function:

setCookie('name', '', -1);
20

Use the jQuery cookie plugin

If you’re using jQuery, you might as well use this cookie plugin. It allows easy management of cookies.

// read $.cookie('name'); // => "Batman" // set $.cookie('name', 'Iron Man'); // delete $.removeCookie('name');
21

document.cookie

First you should understand what the cookie is and how it’s set. A cookie is a string that is stored on the user’s computer to allow data to persist throughout the user’s session. Cookies are often set by HTTP response headers, but they can also be set directly in the browser using JavaScript. To read the entire cookie string you can access document.cookie.

document.cookie

What you’ll likely see is a long string of semi-colon delineated key/value pairs.

ps=31;_ssd3=P0-1415818163-1436218393727;favorite_animal=goat;_gaa=23.00334.....
22

document.cookie

First you should understand what the cookie is and how it’s set. A cookie is a string that is stored on the user’s computer to allow data to persist throughout the user’s session. Cookies are often set by HTTP response headers, but they can also be set directly in the browser using JavaScript. To read the entire cookie string you can access document.cookie.

document.cookie

What you’ll likely see is a long string of semi-colon delineated key/value pairs.

ps=31;_ssd3=P0-1415818163-1436218393727;favorite_animal=goat;_gaa=23.00334.....
23

document.cookie

24

Set a cookie

You can manually set a cookie by setting document.cookie equal to the desired cookie using this format:

document.cookie = "name=tyler;expires=Thu, 03 Sep 2015 05:33:44 GMT";

This can be a little cumbersome, so the function below should make things easier.

function setCookie(name, value, exdays) { var d, expires; exdays = exdays || 1; d = new Date(); d.setTime(d.getTime() + (exdays2460601000)); expires = "expires=" + d.toUTCString(); document.cookie = name + "=" + value + "; " + expires; }

To set a cookie that lasts for one year, you could use:

setCookie('name', 'Batman', 365);
25

Set a cookie

You can manually set a cookie by setting document.cookie equal to the desired cookie using this format:

document.cookie = "name=tyler;expires=Thu, 03 Sep 2015 05:33:44 GMT";

This can be a little cumbersome, so the function below should make things easier.

function setCookie(name, value, exdays) { var d, expires; exdays = exdays || 1; d = new Date(); d.setTime(d.getTime() + (exdays2460601000)); expires = "expires=" + d.toUTCString(); document.cookie = name + "=" + value + "; " + expires; }

To set a cookie that lasts for one year, you could use:

setCookie('name', 'Batman', 365);
26

Set a cookie

27

Read a cookie

To read a cookie you can simply access document.cookie.

console.log(document.cookie);

If you’re searching for a specific value you can use the following function.

function getCookie(name) { var cookie, c; cookies = document.cookie.split(';'); for (var i=0; i < cookies.length; i++) { c = cookies[i].split('='); if (c[0] == name) { return c[1]; } } return ""; }

This function uses

JavaScript’s string split method

to split the cookie string by semi-colons. Then it loops through the result array of strings and splits them again by “=” producing the cookie key/value pair. Then it simply tests the cookie key (c[0]) against the name of the cookie you are trying to read. If it matches it returns the value (c[1]).

28

Read a cookie

To read a cookie you can simply access document.cookie.

console.log(document.cookie);

If you’re searching for a specific value you can use the following function.

function getCookie(name) { var cookie, c; cookies = document.cookie.split(';'); for (var i=0; i < cookies.length; i++) { c = cookies[i].split('='); if (c[0] == name) { return c[1]; } } return ""; }

This function uses

JavaScript’s string split method

to split the cookie string by semi-colons. Then it loops through the result array of strings and splits them again by “=” producing the cookie key/value pair. Then it simply tests the cookie key (c[0]) against the name of the cookie you are trying to read. If it matches it returns the value (c[1]).

29

Read a cookie

30

Delete a cookie

To delete a cookie, you simply need to expire it. So deleting a cookie is basically setting the cookie with an expiration in the past. Using our setCookie function:

setCookie('name', '', -1);
31

Delete a cookie

To delete a cookie, you simply need to expire it. So deleting a cookie is basically setting the cookie with an expiration in the past. Using our setCookie function:

setCookie('name', '', -1);
32

Delete a cookie

33

Use the jQuery cookie plugin

If you’re using jQuery, you might as well use this cookie plugin. It allows easy management of cookies.

// read $.cookie('name'); // => "Batman" // set $.cookie('name', 'Iron Man'); // delete $.removeCookie('name');
34

Use the jQuery cookie plugin

If you’re using jQuery, you might as well use this cookie plugin. It allows easy management of cookies.

// read $.cookie('name'); // => "Batman" // set $.cookie('name', 'Iron Man'); // delete $.removeCookie('name');
35

Use the jQuery cookie plugin