Redirect users who already accepted terms to anoth

2019-05-22 03:54发布

问题:

I ve made a mobile version of a website. Before the user see a specific page he must press a button - CONFIRM, which basically confirms that he accepts the terms of our website. Moreover, as soon as the user press the CONFIRM button he is being redirected to the specific page.

My question is how is it possible to use cookies or localStorage in order to redirect the users who already had been accepted the terms to the specific web page?

I am not quite programming savvy so please be nice :)

回答1:

You can check like this:

if (localStorage.getItem('accepted') === '1') {
   //redirect - already accepted;
}

// after clicking accept

if (accepted === true) {
    localStorage.setItem('accepted', '1');
    //redirect
}

Note that the check and set must be in the same origin.

Demo:
http://jsfiddle.net/AbdiasSoftware/wk9bS/

Of course, you'll need to check that localStorage is available (implicit IMO) and provide a fallback mechanism such as persistent cookies:

if (!!window.localStorage) {
    //use code above

} else {
    //use fallback

}