/    Sign up×
Community /Pin to ProfileBookmark

Delete Cookies

Can someone modify this javascript for me to delete the cookies once the session is over? I thought it was a simple as setting the expiration to 0, however when I refresh my browser I loose all my information.

CODE:
<SCRIPT LANGUAGE=”JavaScript”>
// Cookie Functions //////////////////// (?
// Set the cookie.
// SetCookie(‘your_cookie_name’, ‘your_cookie_value’, exp);
// Get the cookie.
// var someVariable = GetCookie(‘your_cookie_name’);

var expDays = .25;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {

var endstr = document.cookie.indexOf (“;”, offset);

if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {

var arg = name + “=”;

var alen = arg.length;

var clen = document.cookie.length;

var i = 0;

while (i < clen) {

var j = i + alen;

if (document.cookie.substring(i, j) == arg) return getCookieVal (j);

i = document.cookie.indexOf(” “, i) + 1;

if (i == 0) break;

}

return null;
}

function SetCookie (name, value) {

var argv = SetCookie.arguments;

var argc = SetCookie.arguments.length;

var expires = (argc > 2) ? argv[2] : null;

var path = (argc > 3) ? argv[3] : null;

var domain = (argc > 4) ? argv[4] : null;

var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + “=” + escape (value) +
((expires == null) ? “” : (“; expires=” + expires.toGMTString())) +
((path == null) ? “” : (“; path=” + path)) +

((domain == null) ? “” : (“; domain=” + domain)) +

((secure == true) ? “; secure” : “”);
}
// cookieForms saves form content of a page.
// use the following code to call it:
// <body onLoad=”cookieForms(‘open’, ‘form_1’, ‘form_2’, ‘form_n’)” onUnLoad=”cookieForms(‘save’, ‘form_1’, ‘form_2’, ‘form_n’)”>
// It works on text fields and dropdowns in IE 5+
// It only works on text fields in Netscape 4.5

fieldsToSaveList=”author,station,ddate,description,resolution,dtime,dtime1,dtime2,dtime3,dtime4,dtime5,dtime6,dtime7,dtime8,dtime9,dtime10,contract1,contract2,contract3,conctract4,contract5,contract6,contract7,contract8,contract9,contract10,advertiser1,advertiser2,advertiser3,advertiser4,advertiser5,advertiser6,advertiser7,advertiser8,advertiser9,advertiser10,houseno1,houseno2,houseno3,houseno4,houseno5,houseno6,houseno7,houseno8,houseno9,houseno10,isci1,isci2,isci3,isci4,isci5,isci6,isci7,isci8,isci9,isci10,mastercontrol,news,eas,technical”; // change this list to reflect the fields to save
// this line makes a searchable string
fieldsToSave=’@’+ fieldsToSaveList.split(‘,’).join(‘@’)+’@’;
function cookieForms() {

var mode = cookieForms.arguments[0];

for(f=1; f<cookieForms.arguments.length; f++) {
formName = cookieForms.arguments[f];

if(mode == ‘open’) {
cookieValue = GetCookie(‘saved_’+formName);
if(cookieValue != null) {
var cookieArray = cookieValue.split(‘#cf#’);

if(cookieArray.length == document[formName].elements.length) {
for(i=0; i<document[formName].elements.length; i++) {
if (cookieArray[i]) { // if not empty
if(cookieArray[i].substring(0,6) == ‘select’) { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
else if((cookieArray[i] == ‘cbtrue’) || (cookieArray[i] == ‘rbtrue’)) { document[formName].elements[i].checked = true; }
else if((cookieArray[i] == ‘cbfalse’) || (cookieArray[i] == ‘rbfalse’)) { document[formName].elements[i].checked = false; }
else { document[formName].elements[i].value = cookieArray[i]; }
}
}
}
}
}

if(mode == ‘save’) {
cookieValue = ”;
for(i=0; i<document[formName].elements.length; i++) {
fieldType = document[formName].elements[i].type;

fName = document[formName].elements[i].name
if (fieldsToSave.indexOf(“@”+fName+”@”) ==-1) { passValue = ”; }
else if(fieldType == ‘password’) { passValue = ”; }
else if(fieldType == ‘checkbox’) { passValue = ‘cb’+document[formName].elements[i].checked; }
else if(fieldType == ‘radio’) { passValue = ‘rb’+document[formName].elements[i].checked; }
else if(fieldType == ‘select-one’) { passValue = ‘select’+document[formName].elements[i].options.selectedIndex; }
else { passValue = document[formName].elements[i].value; }

cookieValue = cookieValue + passValue + ‘#cf#’;
}
cookieValue = cookieValue.substring(0,cookieValue.length-4); // Remove last delimiter

SetCookie(‘saved_’+formName, cookieValue, exp);

}

}
}
// End –>
</script>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 14.2005 — When I delete cookies, I set the expiraiton date to yesterday's date. You can search this forum for my user name and the keywords "create cookie" to find code I've previously posted for this.
Copy linkTweet thisAlerts:
@felgallJun 14.2005 — If you want a session cookie then create it as a session cookie. Don't set an expiry date in the first place.
×

Success!

Help @wilkeyc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...