2017-10-20 153 views
0

我有一个活动,有2个步骤,每一步有自己的看法,第2步开始时,第一步结束,用户在第二个视图,在第2步,用户不应该能够点击浏览器的后退按钮。 也许它会很好,如果我可以给用户显示一条消息,但我不知道该怎么做。mvc,如何防止点击浏览器的后退按钮?

回答

0

要禁用后退按钮,可以在第二页上使用以下代码。

window.onload = function() { 
if (typeof history.pushState === "function") { 
    history.pushState("jibberish", null, null); 
    window.onpopstate = function() { 
     history.pushState('newjibberish', null, null); 
     // Handle the back (or forward) buttons here 
     // Will NOT handle refresh, use onbeforeunload for this. 
    }; 
} 
else { 
    var ignoreHashChange = true; 
    window.onhashchange = function() { 
     if (!ignoreHashChange) { 
      ignoreHashChange = true; 
      window.location.hash = Math.random(); 
      // Detect and redirect change here 
      // Works in older FF and IE9 
      // * it does mess with your hash symbol (anchor?) pound sign 
      // delimiter on the end of the URL 
     } 
     else { 
      ignoreHashChange = false; 
     } 
    }; 
} 

}

相关问题