-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-cookie.js
More file actions
81 lines (70 loc) · 2.83 KB
/
js-cookie.js
File metadata and controls
81 lines (70 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*********************************************************************
*
* Cookie object
*
* A simple tiny script for using cookie
*
* Created by Derek Erb ( https://derekerb.solutions)
* with 'inspiration' from Kate Morley ( http://code.iamkate.com )
*
* Released under Creative Commons CC0 1.0 Universal
* http://creativecommons.org/publicdomain/zero/1.0/legalcode
*
* del()
* get()
* set()
********************************************************************/
var Cookie = {
/**************************************************************************
* get()
* Retrieve a specific cookie's value
*
* @param strName Name of cookie to be retrieved
*
* RETURNS
* Value of cookie requested
* undefined if cookie does not exist
/************************************************************************/
get : function(strName) {
let matches = document.cookie.match(new RegExp(
"(?:^|; )" + strName.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return(matches ? decodeURIComponent(matches[1]) : undefined);
},
/**************************************************************************
* set()
* Set a cookie.
* Either adds a new cookie or replaces a cookie with the same name on the
* same path
*
* @param strName Name of cookie
* @param strVal Value (to be stored) of cookie
* @param fDays Expiration datetime in number of day
* (Default: 1 day)
* @param strPath Path from which the cookie can be stored/read
* (Default: root /)
* @param bSec Secure only or not?
* If Secure then the cookie only works over an https
* connection
* (Default: false)
/************************************************************************/
set : function(strName, strVal, fDays=1, strPath='/', bSec = false) {
let dt = new Date();
let strExp = 'expires=' + dt.toUTCString(dt.setTime(dt.getTime() + (86400 * 1000 * fDays)));
let strSec = (bSec ? ' secure' : '');
document.cookie = encodeURIComponent(strName) + '=' + encodeURIComponent(strVal) + ';' + strExp + ';path=' + strPath + strSec;
},
/**************************************************************************
* del()
* Delete a specific cookie
*
* @param strName Name of cookie to be deleted
/************************************************************************/
del : function(strName) {
this.set(strName, '', -1);
}
};
Cookie.set('TestCookie', 'Testing1-2-3', 2);
console.log('Cookie=' + Cookie.get('TestCookie'));
Cookie.del('TestCookie');
console.log('Cookie=' + Cookie.get('TestCookie'));