-2

I have this code:

      <div>
            <ul>
              <li id="News">News</li>
              <li id="Messages">Messages</li>
              <li id="Notifications">Notifications</li>
              <li id="Projects">Projects</li>
              <li id="AboutUS">About Us</li>
              <li id="LogOut">Log Out</li>
            </ul>
      </div>
<script type="text/javascript" src="./js/script.js"></script>

script.js:

//Log Out from the application
$("#LogOut").click(function(){
    sessionStorage.removeItem('userName');
    sessionStorage.clear();
    window.location.assign("index.html");
  });

After being logged out and redirected to the page index.html, I clicked the back button of the browser and I was redirected to the last page. I found this Disable browsers back button if the session is invalidated [duplicate] . I tried to add it in the index.html:

<body class="body" onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">            
        <form>

          <input id="login" type="email" class="form-control"  placeholder="Enter login">

          <input id="password" type="password" class="form-control"  placeholder="Password">

          <div class="button_container">
            <button  id ="submit_button" type="button" class="btn btn-outline-secondary"><label for="submit">Login</label></button>
          </div>
        </form>

    <script type="text/javascript" src="./js/script1.js"></script>
</body>

script1.js:

window.history.forward();
function noBack()
{
  window.history.forward();
}

But it doesn't work.How to unable a user to go to the previous page?

para
  • 37
  • 1
  • 9
  • 2
    You don't. The user can reload anything they want from history. What exactly are you trying to accomplish and why? – David Aug 16 '17 at 14:26
  • 2
    On every page where you need to be logged in to visit, do a check to see if the user is logged in. Otherwise, redirect to login or index.html. – Jeff Huijsmans Aug 16 '17 at 14:27

1 Answers1

0

Not sure if this is possible or a good idea. I guess you just dont want the user to go back to the content after logout. You could do something like this:

// To prevent undefined error
    if (localStorage.getItem("auth") == undefined) {
                localStorage.setItem("auth", false)
            }
// functionality
    $(document).ready(function(){
// when logout button is clicked
        $("#logout-btn").click(function(){
            localStorage.setItem("auth", false)
        });
// when auth is not true gets redirected back to the login page or index.html (maybe you could put in a preventDefault otherwise it could get stuck in a loop)
        if(localStorage.getItem("auth") == false){
            window.location.href = "index.html";
        }
    });
// on login set auth to true

hope you get the what i mean. The best way to do it is on Server side with a session cookie.

Tibix
  • 400
  • 3
  • 12