URL redirection after login.

URL redirection after login.

I'm using the following code, where the token is generated on page load.
when the login is done it is redirected to AfterLogin.html page, the below code is for, when user entered to index.html page in another browser tab it will be redirected to AfterLogin.html page, the problem is even after the user is logging out and after page load it is redirecting to AfterLogin.html, How can i prevent that?

  1. $(window).load(function(){
  2. if(localStorage.getItem('token')==null){
  3. $.post("Session.php",function(data){
  4. localStorage.setItem('token',data.Token);
  5. },'json');
  6. }else {
  7. window.location.replace('AfterLogin.html');
  8. }
  9. });