Skip to content

Instantly share code, notes, and snippets.

@yy-dev7
Created March 20, 2017 10:59
Show Gist options
  • Save yy-dev7/52c9e54ab63d835b51cfb5fe49032c47 to your computer and use it in GitHub Desktop.
Save yy-dev7/52c9e54ab63d835b51cfb5fe49032c47 to your computer and use it in GitHub Desktop.
Cookies
const Cookies = {
get(name) {
let start
let end
if (document.cookie.length > 0) {
start = document.cookie.indexOf(`${name}=`)
if (start !== -1) {
start = start + name.length + 1
end = document.cookie.indexOf(';', start)
if (end === -1) end = document.cookie.length
return unescape(document.cookie.substring(start, end))
}
}
return ''
},
set(name, value, expiredays) {
const exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
const exdateString = ((expiredays === null) ? '' : `;expires=${exdate.toGMTString()}`)
document.cookie = `${name}=${escape(value)}${exdateString}`
},
remove(name) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:01 GMT;`
},
}
export default Cookies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment