繁体   English   中英

禁用特定网址的浏览器后退按钮

[英]Disabling browser back button for specific url

我想禁用浏览器的后退按钮,或者说不想允许用户从特定的URL向后导航。 假设网址为:

http://localhost/emptracker/#setup

因此,当用户访问此URL时,我想要的是后退按钮应该被禁用。 现在,我阅读了许多Stack Overflow帖子,并尝试了类似的解决方案

<script type="text/javascript">

    history.pushState(null, null, 'pagename');

    window.addEventListener('popstate', function(event) {
        history.pushState(null, null, 'pagename');
    });

</script>

但是问题是它禁用了整个应用程序中URL的后退按钮,我想对特定的URL进行操作。 我的应用程序是基于beta的。

用它 。 我认为这对您有帮助

file1.html:

<form action="page2.html#no-back" method="post" />
  <input type="text" name="data" value="The data" />
  <input type="submit" value="Send data" />
</form>

file2.html

// It works without the History API, but will clutter up the history
var history_api = typeof history.pushState !== 'undefined'

// The previous page asks that it not be returned to
if ( location.hash == '#no-back' ) {
  // Push "#no-back" onto the history, making it the most recent "page"
  if ( history_api ) history.pushState(null, '', '#stay')
  else location.hash = '#stay'

  // When the back button is pressed, it will harmlessly change the url
  // hash from "#stay" to "#no-back", which triggers this function
  window.onhashchange = function() {
    // User tried to go back; warn user, rinse and repeat
    if ( location.hash == '#no-back' ) {
      alert("You shall not pass!")
      if ( history_api ) history.pushState(null, '', '#stay')
      else location.hash = '#stay'
    }
  }
}

访问本文工作文件

要么

通过在线演示访问本文

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM