javascript 设置cookie然后重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20343496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:16:47  来源:igfitidea点击:

Set cookie then redirect

javascriptredirectcookies

提问by user3040048

The cookie is not holding and the domain example.com cannot redirect to a.example.com when I type on the address bar. Any help will be very much appreciated.

当我在地址栏上输入时,cookie 没有保存并且域 example.com 无法重定向到 a.example.com。任何帮助将不胜感激。

$(function(){
var city = getCookie('city');
if(city !=null && city !=''){
window.location.href = 'http://' + city + '.example.com';
}
$('#citygo').change(function(){
var city = $(this).val();
window.location.href = 'http://' + city + '.example.com';
});
});

<select id="citygo">
<option value="0">Select City</option>
<option value="amsterdam">Amsterdam</option>
<option value="newyork">New York</option>
<option value="london">London</option>
<option value="cardiff">Cardiff</option>
</select>

Is it like this? It is not working. Here is the code:

是这样吗?它不工作。这是代码:

<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
var cookieName = 'city';
var cookieValue = 'city';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + `";domain=.example.com;path=/;expires=" + myDate;
</script>

<script type="text/javascript">
//<![CDATA[
$(function(){
var city = getCookie('city');
if(city !=null && city !=''){
window.location.href = 'http://' + city + '.example.com';
}
$('#citygo').change(function(){
var city = $(this).val();
window.location.href = 'http://' + city + '.example.com';
});
});


function setCookie(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 getCookie(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 dropCookie(name) {
createCookie(name,"",-1);
}
//]]>
</script>

回答by Joe Love

By default, cookies are set to only send on the same domain name that they came from. Be sure you set the cookie domain to .example.com to share across all subdomains (assuming that you indeed are trying to share across subdomains). /

默认情况下,cookies 被设置为只在它们来自的相同域名上发送。确保将 cookie 域设置为 .example.com 以在所有子域之间共享(假设您确实尝试跨子域共享)。/

 $.cookie('city', 'whatevercityhere', { expires: 7, path: '/', domain: '.example.com' });

Edit: Per comments below, also check that the cookie isn't set to http-only.

编辑:根据下面的评论,还要检查 cookie 是否未设置为 http-only。

回答by Mr.G

Try this example:

试试这个例子:

<script type="text/javascript">
var cookieName = 'HelloWorld';
var cookieValue = 'HelloWorld';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate 
                  + ";domain=.example.com;path=/";
</script>