Question

Here's what I'm trying to do:

  • Splash page on subdomain.domain1.com/splashpage.html
  • User clicks a button and is redirected to subdomain.domain1.com/landingpage.html OR subdomain.domain2.com/landingpage.html
  • Cookie set on subdomain.domain1.com/splashpage.html remembers their choice and automatically redirects them next time

I've successfully created the cookie using the jQuery cookies plugin and a very helpful outline here: Remember preferable language

The code I'm using to set the 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> 

A couple problems:

  • My east and west variable URLs appear to be relative to the url variable, they're redirecting to domain1.com/subdomain.domain1.com/landingpage.html
  • On both of my /landingpage.html there is a base href which I cannot edit, so: <base href="http://subdomain.domain1.com/landingpage.html" /> and <base href="http://subdomain.domain2.com/landingpage.html" />

Does anyone know what adjustments I need to make to the code in order to properly redirect users to the correct URL/domain?

Thanks so much.

Was it helpful?

Solution

It is a bit hard to guess what you want, perhaps this is what you are looking for?

$(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');
    });

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top