Skip to content

Instantly share code, notes, and snippets.

@AndrewAllison
Created May 22, 2020 11:14
Show Gist options
  • Save AndrewAllison/5dc47bc0bd4eda7b3e625dbd726261ef to your computer and use it in GitHub Desktop.
Save AndrewAllison/5dc47bc0bd4eda7b3e625dbd726261ef to your computer and use it in GitHub Desktop.
Cookie Reader
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function deleteCookie( name ) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment