2

I'm working on a beer site and trying to get it to go to an age verification page when the site loads using jQuery Cookie.

The age page has a simple Yes to accept the cookie and No to take you away from the site and my code is:

if(!$.cookie('legal-age')){
    window.location = '/age-verify/';
}

// Button
$("a#v-yes").click(function() {
    $.cookie('legal-age','yes', {expires: 7, path: '/'});
    window.location = '/';
    return false;
});

The problem I'm having is when the site loads it goes to the requested page but keeps refreshing over and over.

Any idea how to make it go to that page and just stay without refreshing?

Thanks,

TJ.

Thomas James
  • 687
  • 2
  • 6
  • 21
  • It should be working really?! Hard to see without an example. Have you got a demo anywhere we can help debug? – Rob Schmuecker Jul 02 '14 at 14:21
  • Thanks for that but without being able to "fiddle" the javascript it's hard to properly debug, I strongly suspect it's because you're running the same script on the "age-verify" page and that the cookie (which hasn't been set yet at the point of landing at 'age-verify') is failing the if and sending it into an eternal loop. Have you tried my suggestions from below? – Rob Schmuecker Jul 02 '14 at 14:41

3 Answers3

2

Changing window.location will reload the page.

try to change window.location.hash instead of window.location

Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
Filip Bartuzi
  • 5,711
  • 7
  • 54
  • 102
  • Doesn't seem to work it just opens the home page with the url amended to /#/age-verify/ and doesn't redirect to the /age-verify/ page. – Thomas James Jul 02 '14 at 14:22
1

Judging by your comment above to @Filip Bartuzi,

Try changing your first if to this or variant thereof depending on your paths etc.

if(typeof($.cookie('legal-age')) === 'undefined' && window.location.pathname != '/age-verify'){
    window.location = '/age-verify/';
}
Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
1

I suspect that you have mistakenly included the same code on the page which you are redirecting to.

Make sure that you do not include

if(!$.cookie('legal-age')){
    window.location = '/age-verify/';
}

On the age-verify page, and just the button event handler.

Tim B James
  • 20,084
  • 4
  • 73
  • 103