Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-08-2012, 06:32 PM   PM User | #1
hcrosex3
New Coder

 
Join Date: Sep 2012
Posts: 32
Thanks: 10
Thanked 0 Times in 0 Posts
hcrosex3 is an unknown quantity at this point
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>
hcrosex3 is offline   Reply With Quote
Old 12-08-2012, 06:59 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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!
__________________
"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

Last edited by AndrewGSW; 12-08-2012 at 07:05 PM..
AndrewGSW is offline   Reply With Quote
Old 12-08-2012, 07:07 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 12-08-2012, 10:49 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 12-08-2012, 11:03 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 12-10-2012, 06:57 PM   PM User | #6
mjosh123
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
mjosh123 is an unknown quantity at this point
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
mjosh123 is offline   Reply With Quote
Old 12-10-2012, 07:52 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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" : "" );
}
__________________
"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

Last edited by AndrewGSW; 12-10-2012 at 07:55 PM..
AndrewGSW is offline   Reply With Quote
Old 12-11-2012, 08:28 AM   PM User | #8
mjosh123
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
mjosh123 is an unknown quantity at this point
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 );
}
mjosh123 is offline   Reply With Quote
Old 12-11-2012, 10:27 AM   PM User | #9
misteroram01
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 2 Times in 2 Posts
misteroram01 is an unknown quantity at this point
Actually, turns out the path has to be "/" sometimes
misteroram01 is offline   Reply With Quote
Old 12-11-2012, 11:48 AM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
mjosh123 (12-13-2012)
Old 12-12-2012, 02:52 PM   PM User | #11
mjosh123
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
mjosh123 is an unknown quantity at this point
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.
mjosh123 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:07 PM.


Advertisement
Log in to turn off these ads.