PDA

View Full Version : I need to delete a cookie!



kimberlyhello
09-05-2007, 01:45 AM
I made a javascript that sets a cookie by the name "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.

I should mention that the cookie was written on a different page then the button Im using to delete the cookie. Although both use the same domain name.



<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.

codeexploiter
09-05-2007, 03:50 AM
There is one error in the following code:


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

If you look at the delete_cookie function call, you are supposed to pass a string which is the cookie name you wish to remove in your case "keyword". In JS you need to enclose your string either in single or double quotes.

Try to use the following line instead of your above mentioned line

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

A good tutorial on cookies in JavaScript can be found here (http://www.quirksmode.org/js/cookies.html). You can use the code mentioned in it as it seems to be very simple code to do the cookie based operations.