取消/中止/确认HTML 5状态更改事件(onpopstate)

Man*_*our 6 javascript html5

通过窗口的卸载事件,可以向用户显示确认对话框,假设在有正在等待完成的正在进行的请求的情况下,导航离开页面将终止该请求.

有没有办法用onpopstate的HTML5历史API实现这一目标?或者任何其他具有相同结果的方式?

小智 0

我想您可以修改 PushState 的行为,以在推送新状态之前请求确认:

// Store original pushState
var _pushState = window.history.pushState;
// Some bad global variable to determine if confirmation is needed
var askForConfirm = true;
// Modify pushState behavior
window.history.pushState = function() {
    if(!askForConfirm || confirm('Are you sure you want to quit this page ?')) {
        // Call original pushState
        _pushState.apply(window.history,arguments);
    }
};
Run Code Online (Sandbox Code Playgroud)