作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一些读取 cookie 的帮助。我的网站是双语的,因为我不想根据 IP 更改语言,所以我需要能够设置一个 cookie,我将其称为“_lang”并查看内容是“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';
}
最佳答案
在你的情况下:
var lang_cookie = readCookie('user_lang') ;
if( lang_cookie && lang_cookie !== null && lang_cookie !== 'undefined')
{
if(lang_cookie === 'en')
{
document.location = 'http://en.mysite.com';
}
document.location = 'http://jp.mysite.com';
}
document.location = 'http://en.mysite.com';
关于javascript - 读取 cookie 数据并重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270952/
我是一名优秀的程序员,十分优秀!