/    Sign up×
Community /Pin to ProfileBookmark

deleting a cookie

Hello folks,

I’m doing a website that has both English and Spanish pages. To switch between pages, I am using a button that switches the pages from English To spanish and vice versa. The page will initially load the English page first. Pushing the switcher button “Espanol’ will switch the page to A spanish version of that page, but also set a cookie that will redirect the user to the spanish page next time they go to the site. I am using the code from this tutorial to do this:

[url]http://javascript.internet.com/cookies/cookie-redirect.html[/url]

Here is my code:

[code]

<!– Web Site: The JavaScript Source –>

<!– This script and many more are available free online at –>
<!– The JavaScript Source!! http://javascript.internet.com –>
<!– Original: Ronnie T. Moore –>
<!– Begin
var expDays = 30;
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” : “”);
}

function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() – 1);
var cval = GetCookie (name);
document.cookie = name + “=” + cval + “; expires=” + exp.toGMTString();
}

var favorite = GetCookie(‘language’);

if (favorite != null) {
switch (favorite) {
case ‘sp’ : url = ‘Http://www.zerooneproductions.net/ic/Spanish/home_sp.html’; // change these!
break;
case ‘en’ : url = ‘Http://www.zerooneproductions.net/ic/English/home_en.html’;
break;

}
window.location.href = url;
}

// End –>

[/code]

On English page to set cookie:

[code]
<a href=”#” onclick=”DeleteCookie(‘language’, this.name,exp); getUrl(location.href); return false;” name=”en” class=”languagebtn”></a>
[/code]

Setting the cookie works. I would have like to have been able to not use an absolute path to set my url, however redirect always seemed to include the folder of which the page that set the cookie was in. (ex? instead of redirecting to [url]http://www.zeroneproductions.net/ic/Spanish/home_sp.html[/url] it would redirect me to [url]http://www.zerooneproductions.net/ic/English/Spanish/home_sp.html[/url]. (The page that set the cookie was English/home_en.html)

heres the page: [url]http://www.zerooneproductions.net/ic/English/home_en.html[/url]

If the user decides to switch back to English (from the spanish page using the English button), I would like to delete the cookie but I’m having problems doing this. Can somebody please help.

Thanks for any help you could lend.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@chrysMay 24.2005 — Hi,

To delete a cookie, simply set a cookie with the same name and have the expiration date be in the past.

HEre is my delete cookie function:

[CODE]function delCookie ( name )
{
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime ( cookie_date.getTime() - 1 );
document.cookie = name += "=; expires=" + cookie_date.toGMTString();
}[/CODE]
Copy linkTweet thisAlerts:
@jiveauthorMay 24.2005 — Hello Chrys, and thanks for your reply. I used your code and got the following error:

Line: 30

Char: 1

Error: Object expected

Code: 0

URL: http://www.zerooneproductions.net/ic/Spanish/home_sp.html

I called the function this way:
<i>
</i>&lt;a href="#" onClick="delcookie('language'); getUrl(location.href); return false;" name="en" class="languagebtn"&gt;&lt;/a&gt;


see it here:

http://www.zerooneproductions.net/ic/English/english.html (click espanol to switch) It should bring you to the spanish page where I'm getting the error.
×

Success!

Help @jive 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 5.24,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...