-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.js
More file actions
33 lines (31 loc) · 871 Bytes
/
cookie.js
File metadata and controls
33 lines (31 loc) · 871 Bytes
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
function setCookie( key , value , days){
const d = new Date()
d.setTime( d.getTime() + (days * 86400000 ))
document.cookie = key + '=' + encodeURI( value ) + '; expires=' + d.toUTCString() + ';'
}
function getCookie( key ){
if( document.cookie )
{
const pairs = decodeURI( document.cookie ).split(';')
let i , name , value
for(i = 0 ; i < pairs.length ; i++ )
{
name = ( pairs[ i ].split( '=' )[0]).trim()
if( name === key ){
value = pairs[ i ].split('=')[1]
}
}
return value
}
}
setCookie('User','Mike McGrath,12345', 7)
const list = document.getElementById( 'list' )
let i, value = getCookie( 'User' )
if( value.indexOf(','))
{
value = value.split(',')
}
for( i = 0 ; i < value.length ; i++ )
{
list.innerHTML += '<li>' + value[ i ]
}