这是我正在尝试做的事情:
我已经使用jQuery cookies插件成功创建了cookie,这里有一个非常有用的大纲:Remember preferable language
我用来设置cookie的代码:
<script type="text/javascript">
$(function () {
var url = 'domain1.com';
var east = 'subdomain.domain1.com/landingpage.html';
var west = 'subdomain.domain2.com/landingpage.html';
if ($.cookie('nameofmycookie') != null) {
if (window.location.href != url + '/' + $.cookie('nameofmycookie')) {
window.location.href = url + '/' + $.cookie('nameofmycookie');
}
}
$('#set-eastern').click(function () {
$.cookie('nameofmycookie', east, { expires: 999 });
alert('East was set as your choice');
});
$('#set-western').click(function () {
$.cookie('nameofmycookie', west, { expires: 999 });
alert('West was set as your choice');
});
});
</script>
一些问题:
east
和west
变量网址似乎与url
变量相关,它们会重定向到domain1.com/subdomain.domain1.com/landingpage.html<base href="http://subdomain.domain1.com/landingpage.html" />
和<base href="http://subdomain.domain2.com/landingpage.html" />
是否有人知道我需要对代码进行哪些调整才能将用户正确地重定向到正确的网址/域名?
非常感谢。
答案 0 :(得分:0)
有点难以猜测你想要什么,也许这就是你要找的东西?
$(function () {
var east = 'http://subdomain.domain1.com/landingpage.html';
var west = 'http://subdomain.domain2.com/landingpage.html';
var host = location.hostname;
var cook = $.cookie('nameofmycookie');
if (cook) {
if (cook.indexOf(host)==-1) { // we are not on the site the cookie says
window.location = cook;
}
}
$('#set-eastern').click(function () {
$.cookie('nameofmycookie', east, { expires: 999 });
alert('East was set as your choice');
});
$('#set-western').click(function () {
$.cookie('nameofmycookie', west, { expires: 999 });
alert('West was set as your choice');
});
});