2017-08-16 129 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] 。我試着將它添加在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(); 
} 

不過,這並不work.How到無法在用戶進入到前一頁?

+2

你不知道。用戶可以從歷史中重新加載他們想要的任何東西。你究竟想要完成什麼,爲什麼? – David

+2

在您需要登錄訪問的每個頁面上,請檢查用戶是否已登錄。否則,請重定向至登錄或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。