...

View Full Version : How to delete a cookie???



kimberlyhello
09-04-2007, 08:40 AM
Here is my code for creating and reading cookies:




<script type="text/javascript">


var remembervalues_days=60
var dyncookiename=encodeURI(window.location.pathname)

var recallinput=function(){
var cookienamevalue=rememberinput.getCookie(dyncookiename).split("##")
for (var i=0; i<cookienamevalue.length; i++){
var cookiename=cookienamevalue[i].split("#")[0]
var cookievalue=cookienamevalue[i].split("#")[1]
if (document.getElementById(cookiename)!=null && document.getElementById(cookiename).type=="text")
document.getElementById(cookiename).value=decodeURI(cookievalue)
}
}

var rememberinput=function(){
var rememberit=""
for (var i=0; i<arguments.length; i++){
if (document.getElementById(arguments[i]).type=="text" && document.getElementById(arguments[i]).value!="")
rememberit+=arguments[i]+"#"+encodeURI(document.getElementById(arguments[i]).value)+"##"
}
rememberinput.setCookie(dyncookiename, rememberit, remembervalues_days)
}

rememberinput.getCookie=function(Name){
var re=new RegExp(Name+"=[^;]+", "i");
if (document.cookie.match(re))
return document.cookie.match(re)[0].split("=")[1]
return ""
}

rememberinput.setCookie=function(name, value, days){
var expireDate = new Date()

var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+";";
}

if (window.addEventListener)
window.addEventListener("load", recallinput, false)
else if (window.attachEvent)
window.attachEvent("onload", recallinput)
else if (document.getElementById)
window.onload=recallinput

</script>

</head>
<body>

<form action="" onClick="rememberinput('txt1')" method="get" name="keyword" id="keyword">
<p align="center">
<input name="t" type="" value="" style="visibility:hidden";><br>
Enter Keyword:
<input name="txt1" id="txt1" type="text" size="40" class="memorize">
<input type="button" value="Verify" onClick="iframe();">
</p>
</form>
</body>
</html>


Everything works great so far. Now I want to add a button that will delete the cookie so that people can enter a new keyword. It seems like I have tried everything to get a delete cookie script to work...and I can't figure it out.

Please Help. Thanks.

Philip M
09-04-2007, 08:57 AM
Set the cookie to expire on a date which has already passed.

kimberlyhello
09-04-2007, 10:24 AM
I figured out how to rename the cookie. The name of it is "keyword".

Here is the script I am trying to make work. The button is supposed to delete/rewrite the cookie...but it's not working.



<html>
<head>
<title></title>

<script type="text/javascript">
function delete_cookie ( cookie_name )
{
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime ( cookie_date.getTime() - 1 );
document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
</script>

</head>

<body>
<p align="center"><input type="button" value="Delete Cookie" onclick="delete_cookie(keyword)"></p>
<div align="center"></div>
</body>
</html>





Thank to all that help me.

Philip M
09-04-2007, 10:35 AM
var cookie_date = new Date (); // no space

kimberlyhello
09-04-2007, 07:32 PM
I deleted the extra space, and it still doesn't work.

I should mention that creating and setting the cookie happens on a different page than deleting the cookie. Both pages have the same domain name. I'm not sure if this makes a difference or causes a problem.

Is there anything else I can try??? :)

Philip M
09-05-2007, 08:36 AM
I should mention that creating and setting the cookie happens on a different page than deleting the cookie. Both pages have the same domain name. I'm not sure if this makes a difference or causes a problem.


Yes, it does make a difference. Unless you set the path = "/" then as a path was not specified, it defaults to the path of the document or script that set the cookie.

So:-
document.cookie = 'cookie_name + "; expires= " + cookie_date.toGMTString()" + ";" + " path = /" ' // Note the space after the semi-colon

kimberlyhello
09-05-2007, 10:32 PM
Now I have a new problem. The code I am using is already posted in my first message, but I will post it again:

var remembervalues_days=60
var dyncookiename="keyword"

var recallinput=function(){
var cookienamevalue=rememberinput.getCookie(dyncookiename).split("##")
for (var i=0; i<cookienamevalue.length; i++){
var cookiename=cookienamevalue[i].split("#")[0]
var cookievalue=cookienamevalue[i].split("#")[1]
if (document.getElementById(cookiename)!=null && document.getElementById(cookiename).type=="text")
document.getElementById(cookiename).value=decodeURI(cookievalue)
}
}

var rememberinput=function(){
var rememberit=""
for (var i=0; i<arguments.length; i++){
if (document.getElementById(arguments[i]).type=="text" && document.getElementById(arguments[i]).value!="")
rememberit+=arguments[i]+"#"+encodeURI(document.getElementById(arguments[i]).value)+"##"
}
rememberinput.setCookie(dyncookiename, rememberit, remembervalues_days)
}

rememberinput.getCookie=function(Name){
var re=new RegExp(Name+"=[^;]+", "i");
if (document.cookie.match(re))
return document.cookie.match(re)[0].split("=")[1]
return ""
}

rememberinput.setCookie=function(name, value, days){
var expireDate = new Date()

var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+";";
}

if (window.addEventListener)
window.addEventListener("load", recallinput, false)
else if (window.attachEvent)
window.attachEvent("onload", recallinput)
else if (document.getElementById)
window.onload=recallinput



Now I use this Javascript to create and also to delete the cookie, and it works great. However, I want to be able to create the cookie on one page and delete on another. You said that I would have to make the path = "/"
Where exactly in the above script would I put path = "/"
???

Thanks again.

Philip M
09-06-2007, 09:18 AM
You will find this explained in any textbook.

document.cookie = "name=value; expires=date; path=path;" // note the spaces required after the semi-colons.

path=path is one of the properties we wish to set. This optional value specifies a path within the site to which the cookie applies. Only documents in this path will be able to retrieve the cookie. If this is left blank only the path that set the cookie can retrieve it.

Example: path=/tutorials/ or path = / across the whole site.

kimberlyhello
09-06-2007, 10:21 AM
Thank you so much for your help.

It works perfectly now. I really appreciate your time.

Philip M
09-06-2007, 12:06 PM
Thank you so much for your help.

It works perfectly now. I really appreciate your time.

No problem. Glad your difficulty is resolved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum