0

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.

Community
  • 1
  • 1
user5710
  • 123
  • 7
  • if location.href != "domain1.com/subdomain.domain1.com/landingpage.html" ??? I think you need to change url+"/"+ to "http://"+ – mplungjan Dec 18 '12 at 15:32
  • I just tried that, but for some reason with this code when I return to the splash page with a cookie it redirects me to a version of the URL with the ":" missing from "http://" so the link isn't valid. I've double checked to make sure I included the ":" in my code and I did... – user5710 Dec 18 '12 at 16:03

1 Answers1

0

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

});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Just tried using that code, and the cookie is setting but it's not automatically redirecting me when I hit the splash page. Any idea why that would be? Is there anything I can do to clarify my issue? – user5710 Dec 18 '12 at 20:33
  • So close! The west (domain2.com) link works perfectly. I click the link, the cookie gets set, it takes me to the landing page and when I return to the splash page it also automatically redirects me to the landing page. The east (domain1.com) link however still isn't working perfectly. It works when I click the link, it sets the cookie but then it doesn't redirect me to the landing page when I return to the splash page. Thanks so much for your help so far! – user5710 Dec 19 '12 at 14:09
  • Got it! Just one small tweak to the above code was to make the var east URL relative (just landingpage.html) and it seems to have worked. Thanks so much. – user5710 Dec 19 '12 at 14:21