Set a cookie
document.cookie="username=John Doe";
Get a cookie
var x = document.cookie;
Function Based
<script type="text/javascript">
/**
* Set Cookie
*
* @param string cname the name of the cookie
* @param string cvalue value to be set
* @param string cdays number of days before it expires
*
*/
function setCookie(cname, cvalue, cdays) {
var d = new Date();
d.setTime(d.getTime() + (cdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/**
* Set Cookie by path
*
* @param string cname the name of the cookie
* @param string cvalue value to be set
* @param string cdays number of days before it expires
* @param string cpath directory path, ex. "/" to apply to the entire domain
*
*/
function setCookieByPath(cname, cvalue, cdays, cpath) {
var d = new Date();
d.setTime(d.getTime() + (cdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires + "; path=" + cpath;
}
/**
* Get Cookie
*
* @param string cname the name of the cookie to retrieve
*
*/
function getCookie(cname) {
var value = "; " + document.cookie;
var parts = value.split("; " + cname + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
</script>