Javascript Browser Cookies - Deleting a Cookie
|
Remove a Cookie - Javascript
The javascript cookie methods on these pages demonstrate how to:- set/put a cookie - Try it
- get/read a cookie - Try it
- delete/remove a cookie - Try it
- restrict webpage access based on a cookie - Try it
Delete the Cookie
This page demonstrates one method of effectively deleting a client-side cookie using Javascript. For this example, we will 'expire' the cookie, by changing the cookie's expiration date to the August 19, 1996. After the cookie has been deleted, you can generate a new cookie on the Setting a Javascript Cookie page. If a brenz.net cookie is present on your machine, you'll see a button below which will allow you to Delete the Cookie.
Examining cookie...
To delete (expire) the cookie, simply put a call to
the javascript function in your HTML page. In my example, I attach the javascript function
to the onclick() event of an input button:
<input type="button" value="Delete Cookie" onclick="javascript:cookieDelete();" />
The cookieDelete() javascript function needs to be defined, either before the ending
<head> tag in the current document or in an external javascript file, as follows:
var cookieText = "Put your desired cookie value here";
var cookiePrefix = "";
var myPage = location.href;
var wwwFlag = myPage.indexOf('www');
if (wwwFlag > 0) {
cookiePrefix = "www";
}
var cookieName = cookiePrefix + "cbCookie";
function cookieDelete() {
if (document.cookie != document.cookie) {
index = document.cookie.indexOf(cookie_name);
} else {
index = -1;
}
if (index == -1) {
document.cookie=cookie_name+"=GONEcbEndCookie; expires=Monday, 19-Aug-1996 05:00:00 GMT";
}
document.location.href = document.location.href;
//to reload without queryString parameters, use the line below instead
//document.location = document.location.href.substring(0,document.location.href.indexOf("?"));
}
Related Pages:
var cookiePrefix = "";
var myPage = location.href;
var wwwFlag = myPage.indexOf('www');
if (wwwFlag > 0) {
cookiePrefix = "www";
}
var cookieName = cookiePrefix + "cbCookie";
function cookieDelete() {
if (document.cookie != document.cookie) {
index = document.cookie.indexOf(cookie_name);
} else {
index = -1;
}
if (index == -1) {
document.cookie=cookie_name+"=GONEcbEndCookie; expires=Monday, 19-Aug-1996 05:00:00 GMT";
}
document.location.href = document.location.href;
//to reload without queryString parameters, use the line below instead
//document.location = document.location.href.substring(0,document.location.href.indexOf("?"));
}