Skip to content

Instantly share code, notes, and snippets.

@jerry4
Created December 12, 2014 21:31
Show Gist options
  • Save jerry4/5a3f7c19798c38ab7ace to your computer and use it in GitHub Desktop.
Save jerry4/5a3f7c19798c38ab7ace to your computer and use it in GitHub Desktop.
Vanilla js to get cookies
// from stackoverflow: http://stackoverflow.com/questions/5639346/shortest-function-for-reading-a-cookie-in-javascript
(function(){
var cookies;
function readCookie(name,c,C,i){
if(cookies){ return cookies[name]; }
c = document.cookie.split('; ');
cookies = {};
for(i=c.length-1; i>=0; i--){
C = c[i].split('=');
cookies[C[0]] = C[1];
}
return cookies[name];
}
window.readCookie = readCookie; // or expose it however you want
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment