JavaScript : Cookies Manipulation (ADD / UPDATE / DELETE)

Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.
A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they handle the optional attributes, so use them sparingly to improve the inter-operability of your servlets.
The servlet sends cookies to the browser by using the  HttpServletResponse.addCookie(javax.servlet.http.Cookie) method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.

function readCookie(name) {
var nameEQ = escape(name) + "=";
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, c.length);
if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));
}
return null;
}

function deleteCookie(cookieName,cookieValue)
{
var now = new Date();
now.setMonth( now.getMonth() - 1 ); 
cookievalue = escape(cookieValue);
document.cookie=cookieName+"="+ cookievalue
+ ";expires=" + now.toGMTString() + "; path=/; secure;domain=ktpot.com"; //secure only for https
}

function setCookie(cookieName,cookieValue,nDays) 
{
var today = new Date();
var expire = new Date();

//deleteCookie(cookieName,cookieValue);    

if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString()+"; path=/; secure;domain=ktpot.com"; //secure only for https

}

0 comments: