javascript cookies
Delete a Cookie - Javascript
The javascript cookie methods on these pages demonstrate how to:- set/put a cookie
- get/read a cookie
- delete/remove a cookie
- restrict webpage access based on a cookie
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:
[the end]
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("?"));
}
The method(s) on this page have been tested on Windows XP, using
Internet Explorer 6.0, Firefox 1.5, and Netscape 7.2.
Popup Windows
This page covers the basics of popup windows - opening, closing, and changing URLs.
Javascript Cookies
Learn the basics of manipulating cookies with javascript - setting, getting, deleting.
VoiceXML Demo
A simple, toll-free demonstration of telephone and web application interaction.
©2007 chris brenz, brenz.net

