我需要一些帮助来阅读 cookie。我的网站是双语的,因为我不想根据 IP 更改语言,所以我需要能够设置一个名为“_lang”的 cookie,并查看内容是“en”还是“jp”。我可以设置 cookie 及其内容,没有问题,但我无法确定读取 cookie 内容的语法;这是我第一次真正尝试过。
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
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 "here";
i=document.cookie.indexOf(" ",i)+1;
if (i==0) break;
}
return null;
}
var visit=getCookie("_lang");
var rc=readCookie("_lang");
if (visit==true){
if (rc == "jp"){
window.location = "http://jp.mywebsite.com";
}
else if (rc == "en"){
window.location = "http://www.mywebsite.com";
}
}
else if (visit==null){
document.title = "Welcome to MyWebsite!"
document.getElementById('welcomeLB').style.display='block';
document.getElementById('behindLightBox').style.display='block';
}