Skip to content

Instantly share code, notes, and snippets.

@jyeary
Last active September 25, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyeary/b9b549dba1ff8836d6eb to your computer and use it in GitHub Desktop.
Save jyeary/b9b549dba1ff8836d6eb to your computer and use it in GitHub Desktop.
Javascript for reading a cookie
//<![CDATA[
/**
* This function returns the named cookie if it exists.
* @param {type} cname The name of the cookie.
* @returns {String} The cookie value.
*/
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
//]]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment