Skip to content

Instantly share code, notes, and snippets.

@zhongdeliu
Last active August 4, 2016 07:24
Show Gist options
  • Save zhongdeliu/4f470f26d5e4f9cc182a69888fab08c0 to your computer and use it in GitHub Desktop.
Save zhongdeliu/4f470f26d5e4f9cc182a69888fab08c0 to your computer and use it in GitHub Desktop.
Cookie methods
function readCookie(name) {
name += '=';
for (var ca = document.cookie.split(/;\s*/), i = ca.length - 1; i >= 0; i--)
if (!ca[i].indexOf(name))
return ca[i].replace(name, '');
}
// https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie/Simple_document.cookie_framework
function readCookie(sKey) {
if (!sKey) { return null; }
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment