0

Is there an easy way to store variables in javascript so I can know that the user tried to visit that specific page?

It's a pretty complex system that log in users on Shopify and Wordpress at the same time.

So I would like to store a global variable that check's if the user visit page /pages/professionals-only and if user get to this page again, but now logged in, it can be run a specific code.

1 Answers1

1

There are multiple ways to go about this. (Also, do some research, please). 1.

if(window.localStorage){
  if(window.location == '/pages/professionals-only'){
    if(window.localStorage.getItem('visited') == true){
      //they have already visited the page. Do something here
    }else{
      window.localStorage.setItem('visited',true)
    }
  }
}

read about local storage

Way 2.

if(window.sessionStorage){
  if(window.location == '/pages/professionals-only'){
    if(window.sessionStorage.getItem('visited') == true){
      //they have already visited the page. Do something here
    }else{
      window.sessionStorage.setItem('visited',true)
    }
  }
}

read about sessionStorage Way 3.

if(window.location == '/pages/professionals-only'){
  if(document.cookie.split('=')[1] == 'true'){
    //they have already visited the page. Do something here
  }else{
    document.cookie = 'visited=true';
  }
}

read about cookies Hope this is what you were looking for. If it is not, please comment. You can also create simple login systems using this technique. Such as window.sessionStorage.setItem('username','bob'); and window.sessionStorage.getItem('username')

SoJS
  • 123
  • 1
  • 9