1

window.addEventListener('popstate', function(event) {
   alert("you are not able to push back button");
 });

我已经使用聚合物 2.0 创建了 Web 应用程序,但是我必须单击后退按钮才能退出浏览器如果用户单击浏览器的后退按钮,我必须显示警报我尝试过 window.addEventListener 但仍然得到错误。

4

1 回答 1

0

我无法停止浏览器的后退按钮,但我设法绕过它。在我的应用程序中,我想通过备份到第一页来警告用户他们将注销,并让他们有机会离开或留在原地。使用polymer-2-starter-kit作为我的起点,并跟踪一个connected属性,我得到了这个工作:

_routePageChanged(page) {
  // If no page was found in the route data, page will be an empty string.
  // Default to 'home' in that case.
  this.page = (page && this.connected) ? page : 'home';

  // Close the drawer.
  this.drawerOpened = false;
}

_pageChanged(page, oldPage) {
  // Warn user if backing up logs out.
  if ((page == '' || page == 'home') && this.connected) {
    if (window.confirm("Do you really mean to logout?")) {
      this.$.xhrLogout.generateRequest();
    } else {
      window.history.forward();
    }
  }
  const resolvedPageUrl = this.resolveUrl('my-' + page + '.html');
  Polymer.importHref(
      resolvedPageUrl,
      null,
      this._showPage404.bind(this),
      true);
}

因此,如果用户已连接并导航到初始页面,我可以强制他们停留在他们所在的页面上window.history.forward()

于 2018-03-17T22:41:01.110 回答