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 11 of 11
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Delete all cookies?

    I currently have a function to expire the cookie when the user logs off. As of now it is not is not expiring the cookie any suggestions?

    JS
    Code:
    /// Validate Field
    
    function catchEvent(eventObj, event, eventHandler) {
      if (eventObj.addEventListener) {
        eventObj.addEventListener(event, eventHandler, false);
      } else if (eventObj.attachEvent){
        event = "on" + event;
        eventObj.attachEvent(event, eventHandler);
      }
    }
    catchEvent(window,"load", setupEvents);
    
    function setupEvents(evnt)
    {
    catchEvent(document.getElementById("txt"), "blur", validateField)
    }
     function validateField ()
    {
        var val = this.value.replace(/\D/g, "" ); // zap all NON-digit characters
    
    
        if ( val.length != 7 )  /* then there must be 7 digits exactly */
        {
            this.value = '';
      alert("not a vln");
            return false; // ?? may not be needed, can't hurt
        } 
        this.value = val.substr(0,2) + "-" + val.substr(2,2) + "-" + val.substr(4);
          document.cookie="Vln=" + this.value;
             document.getElementById('txt').style.visibility= 'hidden'; 
             document.getElementById('button').style.visibility= 'visible'; 
    
    
    
        return true;
    }
    
    
    /*
    Cookies
    */
    
    function ReadCookie()
    {
       var allcookies = document.cookie;
    
       // Get all the cookies pairs in an array
       cookiearray  = allcookies.split(';');
    if (allcookies != '') {
       // Now take key value pair out of this array
       for(var i=0; i<cookiearray.length; i++){
          name = cookiearray[i].split('=')[0];
          value = cookiearray[i].split('=')[1];
    
          return "VLN:" + value;
       } 
         }
    
     else {
      return "";
     }      
        
    }
    
    function deleteCookie(ReadCookie) {
    var exp = new Date(); // create a new Date object
    exp.setTime(exp.getTime() - 1); // set it to yesterday!
    var cval = "";
    document.cookie = allcookies + "=" + cval + "; expires=" + exp.toGMTString();
    } // end deleteCookie





    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    
    
    
    
    <head>
      <meta charset="UTF-8">
    
    <script type="text/javascript" src="fadeslideshow.js">
    </script>
    
    	  <link href="stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css" />
      <link href="stylesheets/print.css" media="print" rel="stylesheet" type="text/css" />
      <!--[if IE]>
          <link href="stylesheets/ie.css" media="screen, projection" rel="stylesheet" type="text/css" />
      <![endif]-->
    <link href='http://fonts.googleapis.com/css?family=Italianno' rel='stylesheet' type='text/css'>	
    
    
    </head>
    
    
    
    
    
    
    <body >
    	<div id="header"> <h1> Rose Photography</h1> </div>
    	<div class="nav">
    <ul>
    <li><a href="index.htm" >Home</a></li>
    <li><a href="About.htm">About</a></li>
    <li><a href="contact.htm" >Contact</a></li>
    <li><a href="404.htm">Past Bookings</a></li>
    </ul>
    </div>
    <div id="log">
      <script> document.write(ReadCookie());</script> 
    
    </div>
    <div id="login"> 
    	<input type="text|hidden" id="txt" name="txt"onblur="validateField()" >  
      <button type="button" id="button" name="button" style= "visibility:hidden ;" onclick="deleteCookie()"> Log Off </button> </div> 
    
      </body>

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    In the following code

    Code:
    document.cookie = allcookies + "=" + cval + "; expires=" + exp.toGMTString();
    allcookies should be the name of the specific cookie that you are deleting. [You don't need to set the 'name=value', just 'name=;expires=santasdayout']

    BTW the following only subtracts 1 millisecond, but it is immaterial

    Code:
    exp.setTime(exp.getTime() - 1); // set it to yesterday!
    But you should also validate your HTML as you are missing the html tag, and "text|hidden" is incorrect.

    BTW I don't think 'button' is a sensible name for a button .. I know it's a button!
    Last edited by AndrewGSW; 12-08-2012 at 07:05 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    This is much easier
    Code:
    function del_cookie(name) {
        document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    }
    if you don't need to worry about the path or domain.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,274
    Thanks
    10
    Thanked 581 Times in 562 Posts
    i had some that wouldn't go away. turns out the path has to be "/" sometimes.
    it's best to sweep through all keys with and without the path, nulling them all out.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by rnd me View Post
    i had some that wouldn't go away. turns out the path has to be "/" sometimes.
    it's best to sweep through all keys with and without the path, nulling them all out.
    Yes, I recall now that I had to include "/" previously.

    Code:
    function deleteCookie ( name, path, domain ) {
        document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +
            ( ( domain ) ? ";domain=" + domain : "" ) +
            ";expires=Fri, 01-Jan-2010 00:00:01 UTC";
    }
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I used mentioned code to modify the cookie value. I was getting two cookies from production server. I have to modify the value of one cookie.
    firstly I stored the value & names of the cookies from document.cookie. Then deleted the specific cookie thru your mentioned code


    function deleteCookie ( name, path, domain ) {
    document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Fri, 01-Jan-2010 00:00:01 UTC";
    }

    and lastly I modified the value of the cookie as I have stored it in array. Finally creating cookie using document.cookie

    what it has done is, my cookie file created at (C:\Documents and Settings\Administrator\Cookies) is containing only one cookie

    could u pls tell me how can I get value updated in this file too.

    Thanks in advance

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I'm not sure what you are asking: is there a problem with your code, or is there something additional you are trying to achieve?

    I notice this code in your first post:
    Code:
    document.cookie="Vln=" + this.value;
    This creates a cookie but doesn't set an expiration, so it will be deleted when the browser is closed. Perhaps this is your issue(?).

    In which case you probably want a function to set a cookie:

    Code:
    function setCookie( name, value, expires, path, domain, secure ) {
        // Sets the name/value pair - 'expires' is the number of days.
        var expires_date;
        if (expires) {
            expires_date = new Date();
            expires_date.setDate(expires_date.getDate() + expires);
        }
        document.cookie = name + "=" + value +
            ( ( expires ) ? ";expires=" + expires_date.toUTCString() : "" ) +
            ( ( path ) ? ";path=" + path : "" ) +
            ( ( domain ) ? ";domain=" + domain : "" ) +
            ( ( secure ) ? ";secure" : "" );
    }
    Last edited by AndrewGSW; 12-10-2012 at 07:55 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #8
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Andrew,
    I am not the same user for whom you provided the solution. I am actually new user, working on cookie value updation.

    I basically need to create a unique cookie for users browsing through single computer.
    I have created a subdomain of my client server on other linux server and kept my html pages under the cgi-bin folder of apache server
    the apache server is running on port 1080. also I have modified my system hostfile (added name of the my subdomain with IP)
    this is the URL type I am giving at my browser.
    http://subdomain.domainname.com:port...yhtmlpage.html

    Now doing so, client server will send cookie with 2 cookies (X=value;Y=value). for making unique cookie I need to update the value of Y cookie with some value
    I am reading the cookie via document.cookie. then storing both the values in an array. and later seprating the value and name from name value pair stored in array.
    Once this is done, I am deleting the Y cookie by setting the expiry date and then using document.cookie to create a new Y cookie with new value.

    Now In doing so when I am printing the old value, deleted value and new value through document.write. everything is happening as intended
    but when I checked the cookie stored at location (). firstly it was storing both the values (x and y) When I deleted Y, it was having only X in the file
    but when I created Y with new value. it didnt got reflected.

    Code snippet is below
    document.write("cookie we have is " + document.cookie + "<br>");
    var allcookies = document.cookie;
    var cookiearray = allcookies.split(';');
    for(var i=0; i<cookiearray.length; i++){
    var Cookiename = cookiearray[i].split('=')[0];
    var Cookievalue = cookiearray[i].split('=')[1];
    alert("Key is : " +Cookiename + " and Value is : " + Cookievalue);
    }

    DelCookie(Cookiename);
    UpdateCookie(Cookievalue);

    function DelCookie(Cookiename)
    {
    document.cookie = Cookiename + "=" + " " + ";expires=Fri, 01-Jan-2010 00:00:01 UTC" ;
    document.write("cookie we have is " + document.cookie + "<br>");
    }

    function UpdateCookie(Cookievalue)
    {
    var loginid = 100020002;
    var newCookievalue = Cookievalue.concat(loginid);
    document.cookie=Cookiename + "=" + newCookievalue;
    document.write("cookie we have is " + document.cookie + "<br>");
    alert ("New Value is " +newCookievalue );
    }

  • #9
    New Coder
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Actually, turns out the path has to be "/" sometimes

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    document.write("cookie we have is " + document.cookie + "<br>");
    var allcookies = document.cookie;
    var cookiearray = allcookies.split(';');
    for(var i=0; i<cookiearray.length; i++){ // this is looping through all cookies
    var Cookiename = cookiearray[i].split('=')[0]; // ..so these will end up storing the name and value
    var Cookievalue = cookiearray[i].split('=')[1]; // ..of whatever is the last cookie
    alert("Key is : " +Cookiename + " and Value is : " + Cookievalue);
    }
    
    DelCookie(Cookiename); // so this will delete the LAST cookie
    UpdateCookie(Cookievalue);
    
    function DelCookie(Cookiename)
    {
    document.cookie = Cookiename + "=" + " " + ";expires=Fri, 01-Jan-2010 00:00:01 UTC" ;
    document.write("cookie we have is " + document.cookie + "<br>");
    }
    
    function UpdateCookie(Cookievalue)
    {
    var loginid = 100020002;
    var newCookievalue = Cookievalue.concat(loginid);
    document.cookie=Cookiename + "=" + newCookievalue;
    document.write("cookie we have is " + document.cookie + "<br>");
    alert ("New Value is " +newCookievalue );
    }
    Also, as mentioned in other posts, you should use a version of a deleteCookie function that allows the specifying of the path, usually as "/". My function would be used like this:
    Code:
    deleteCookie(someName, '/');
    You should wrap your code in CODE tags by clicking the hash (#) key when creating your posts.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    mjosh123 (12-13-2012)

  • #11
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by misteroram01 View Post
    Actually, turns out the path has to be "/" sometimes

    I set the path to "/" but nothing changed [I mean after updating cookie value, the new value is not getting updated in cookie file (administrator@cgi-bin[1])stored at location C:\Documents and Settings\Administrator\Cookies]

    Can anyone please let me know, which path is it ?? I mean, does it has something to do with the windows path.


  •  

    Posting Permissions

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