2017-08-16 9 views
-2

私はこのコードを持っている:javascriptを使ってブラウザの戻るボタンを削除するには?

 <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"); 
    }); 

ログアウトし、ページindex.htmlにリダイレクトされた後、私は、ブラウザの[戻る]ボタンをクリックし、私はにリダイレクトされました最後のページ。私はこれを見つけたDisable browsers back button if the session is invalidated [duplicate]

<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:私はindex.htmlの中でそれを追加しようとした

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

をしかし、それは、前のページに移動することができません。ユーザーにwork.Howないのですか?

+2

あなたはそうではありません。ユーザーは履歴から必要なものをリロードできます。あなたは正確に何を達成しようとしていますか、なぜですか? – David

+2

ログインする必要があるすべてのページで、ユーザーがログインしているかどうかを確認します。それ以外の場合は、loginまたはindex.htmlにリダイレクトします。 –

答えて

0

これが可能かどうかはわかりません。 ログアウト後にユーザーがコンテンツに戻ってほしくないと思う。 あなたはこのような何か行うことができます:

// 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 

をあなたは私が何を意味するかを取得願っています。 これを行う最善の方法は、サーバー側でセッションCookieを使用することです。

関連する問題