Home > OS >  How to redirect new visitor to a landing page?
How to redirect new visitor to a landing page?

Time:08-13

So basically i need a redirect to a landing page but this code: How to redirect first-time visitors to another page? worked when I tested it first and now it's not working and I even tested the ones in the comments or reply.. please help. Creating the cookies script is not working

CodePudding user response:

You can use LocalStorage instead of cookie:

if(!("visited" in localStorage)){
    localStorage.visited="";
    window.location="https://google.com/";
}

CodePudding user response:

You can use localStorage instead of cookie. It has 5 mb limit of memory per domain. First check if isNewVisitor is null .If the user never visited the site, then set it to true and redirect to you homepage. if the user visits 2nd or 3rd time, it goes else if block and sets isNewVisitor to false.

var homePage= 'www.yourwebsite.com'

if(localStorage.getItem('isNewVisitor') === null){
      localStorage.setItem('isNewVisitor') = true;
      window.location = homepage;

}else if(localStorage.getItem('isNewVisitor')){
 localStorage.setItem('isNewVisitor') = false;
}
  • Related