...

View Full Version : Delete all cookies?



hcrosex3
12-08-2012, 07:32 PM
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


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







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

AndrewGSW
12-08-2012, 07:59 PM
In the following 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 ;)


exp.setTime(exp.getTime() - 1); // set it to yesterday!
But you should also validate (http://validator.w3.org/#validate_by_input) 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!

AndrewGSW
12-08-2012, 08:07 PM
This is much easier

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.

rnd me
12-08-2012, 11:49 PM
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.

AndrewGSW
12-09-2012, 12:03 AM
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.


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

mjosh123
12-10-2012, 07:57 PM
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

AndrewGSW
12-10-2012, 08:52 PM
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:

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:


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" : "" );
}

mjosh123
12-11-2012, 09:28 AM
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:portnumber/cgi-bin/myhtmlpage.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 );
}

misteroram01
12-11-2012, 11:27 AM
Actually, turns out the path has to be "/" sometimes

AndrewGSW
12-11-2012, 12:48 PM
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:

deleteCookie(someName, '/');
You should wrap your code in CODE tags by clicking the hash (#) key when creating your posts.

mjosh123
12-12-2012, 03:52 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum