重定向到多个域的jQuery cookie

时间:2012-12-18 15:16:02

标签: jquery jquery-cookie

这是我正在尝试做的事情:

  • subdomain.domain1.com/splashpage.html
  • 上的启动页面
  • 用户点击按钮并重定向到subdomain.domain1.com/landingpage.html OR subdomain.domain2.com/landingpage.html
  • 在subdomain.domain1.com/splashpage.html上设置的Cookie会记住他们的选择并在下次自动重定向

我已经使用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> 

一些问题:

  • 我的eastwest变量网址似乎与url变量相关,它们会重定向到domain1.com/subdomain.domain1.com/landingpage.html
  • 在我的两个/landingpage.html上都有一个我无法编辑的基本href,因此:<base href="http://subdomain.domain1.com/landingpage.html" /><base href="http://subdomain.domain2.com/landingpage.html" />

是否有人知道我需要对代码进行哪些调整才能将用户正确地重定向到正确的网址/域名?

非常感谢。

1 个答案:

答案 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');
    });

});