Results 1 to 2 of 2

Thread: I need to delete a cookie!

  1. #1
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need to delete a cookie!

    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.

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    There is one error in the following code:

    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
    Code:
    <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. You can use the code mentioned in it as it seems to be very simple code to do the cookie based operations.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •