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
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 –>
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>
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]http://www.zerooneproductions.net/ic/English/Spanish/home_sp.html
heres the page: [url]http://www.zerooneproductions.net/ic/English/home_en.html
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.