N
nic den
hello,
I'm calling a function to delete a cookie when a page is left.
The problem is when I quickly push the reload button of my browser. The
browser doesn't understand what's happening and don't call my exit function,
maybe not enough time ?
How could I solve this problem in order to be sure that finally the cookie
is deleted ?
thanks a lot
nicolas
ps. here's my code:
function getCookie(name) {
var dc = window.document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = window.document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
var cookieVal = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
window.document.cookie = cookieVal;
window.top.document.cookie = cookieVal;
window.ff_top.document.cookie = cookieVal;
window.ff_left.document.cookie = cookieVal;
window.ff_ct.document.cookie = cookieVal;
}
}
function exitPage() {
alert(window.document.cookie);
deleteCookie("moncookiedetest","/","");
}
window.onunload=exitPage;
I'm calling a function to delete a cookie when a page is left.
The problem is when I quickly push the reload button of my browser. The
browser doesn't understand what's happening and don't call my exit function,
maybe not enough time ?
How could I solve this problem in order to be sure that finally the cookie
is deleted ?
thanks a lot
nicolas
ps. here's my code:
function getCookie(name) {
var dc = window.document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = window.document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
var cookieVal = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
window.document.cookie = cookieVal;
window.top.document.cookie = cookieVal;
window.ff_top.document.cookie = cookieVal;
window.ff_left.document.cookie = cookieVal;
window.ff_ct.document.cookie = cookieVal;
}
}
function exitPage() {
alert(window.document.cookie);
deleteCookie("moncookiedetest","/","");
}
window.onunload=exitPage;