0

I have an age restricted modal and when the user clicks "no" it goes back a page in their history, but what if a user accessed the webpage on a fresh tab? They don't have a history? Can the button take them to a specific url?The browser's default homepage? Close the tab?

This is what I currently have:

<a onClick="goBack()">Go Back</a>

<script>
    function goBack() {
      window.history.back();
      return false;
    }
</script>

In terms of UX, out of the three scenarios I've given, I mean, I'm not sure what's the best, but I'm open to whatever is the most clean to accomplish.

Darren
  • 177
  • 2
  • 9
  • So than just redirect them somewhere else..... `window.location.href="https://www.youtube.com/watch?v=1lbYHw-MHSo"` – epascarello Nov 02 '18 at 21:03
  • What seems most logical from UX perspective is to keep your code like it is, but just check if `history.length === 0` and in the case it is `0` ( https://stackoverflow.com/questions/3588315/how-to-check-if-the-user-can-go-back-in-browser-history-or-not ) you can just disable the button at all and leave the user with a message that he can't proceed. – drinchev Nov 02 '18 at 21:05

2 Answers2

0

If you have a specific page you want to return to, you can use location.replace or location.assign. Looks like this:

window.location.replace("https://mywebsite.com"); // does not create a point in Browser History

window.location.assign("https://mywebsite.com"); // creates a point in Browser History

Generally, this is considered best practice unless your homepage is also age inappropriate, then you probably want to make a specific "Good By" page.

Nosajimiki
  • 1,073
  • 1
  • 9
  • 17
0

In terms of UI, I think that doing the user back in the browser history is wrong because it's not what the user really want to do. On the other hand, close the tab it's not what the user wants as well.

So, what is the ideal solution??

I would make a landing page for the user, and in that landing page, I would open a modal window asking the user age. If the user selects "no", I would save his answer in a local storage browser and close the modal, showing basic safe information about your site.

This way, in the next time the user tries to enter in your page, it will not see the modal anymore, only the safe web information. I believe this is a correct navigation solution. The user should decide if he(she) wants to close the tab or not, back to history or not, and so on.

Pablo Darde
  • 5,844
  • 10
  • 37
  • 55
  • Saving results is great for sites that handle user profiles, but I would save that in the user profile, not in the browser if that is an option. I'd generally avoid this behavior though for profileless sites because people often share computers. For example, if a page is tagged PG-13, and a younger brother says he's 11, you would lock out his older 15 year old brother which would be another unwanted behavior. – Nosajimiki Nov 02 '18 at 21:15
  • I agree, in this case, would be nice if you have an option(like a button) "ENTER" , allowing the user to see the modal again and choose your correct age profile. – Pablo Darde Nov 02 '18 at 21:19
  • A lot of this will all depend on what you are trying to hide. That could be a decent work around for a userless system like a porn site where you are unlikely to get people wanting to create accounts, but still need to verify appropriate usage. An NSFW or PG section of a service intended for all ages should probably be profile restricted though. – Nosajimiki Nov 02 '18 at 22:13