Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to delete a cookie???

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Set the cookie to expire on a date which has already passed.

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ok...here is what I have now.

    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    var cookie_date = new Date (); // no space

  • #5
    New Coder
    Join Date
    Mar 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    It still doesn't work

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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by kimberlyhello View Post
    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
    Last edited by Philip M; 09-05-2007 at 07:58 AM.

  • #7
    New Coder
    Join Date
    Mar 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank You!

    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.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #9
    New Coder
    Join Date
    Mar 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    Thank you so much for your help.

    It works perfectly now. I really appreciate your time.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by kimberlyhello View Post
    Thank you so much for your help.

    It works perfectly now. I really appreciate your time.
    No problem. Glad your difficulty is resolved.


  •  

    Posting Permissions

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