window.addEventListener('popstate', function(event) {
alert("you are not able to push back button");
});
我已经使用聚合物 2.0 创建了 Web 应用程序,但我必须单击浏览器的后退按钮才能注销,如果用户单击浏览器的后退按钮,我必须显示警报,我已经尝试了 window.addEventListener 但仍然收到错误。
我无法停止浏览器的后退按钮,但我已经设法绕过了它。在我的应用程序中,我想警告用户,他们将通过备份到第一页来注销,并给他们一个离开或留在原地的机会。使用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()
.