【发布时间】:2013-08-11 02:13:46
【问题描述】:
需要在点击浏览器后退按钮时创建 javascript 确认弹出窗口。如果我点击后退按钮,会弹出“你想继续吗?”如果点击是,那么它会重定向到上一页。
我有以下代码,它没有按要求工作。
if(window.history && history.pushState){ // check for history api support
window.addEventListener('load', function(){
// create history states
history.pushState(-1, null); // back state
history.pushState(0, null); // main state
history.pushState(1, null); // forward state
history.go(-1); // start in main state
this.addEventListener('popstate', function(event, state){
// check history state and fire custom events
if(state = event.state){
event = document.createEvent('Event');
event.initEvent(state > 0 ? 'next' : 'previous', true, true);
this.dispatchEvent(event);
var r = confirm("Would you like to save this draft?");
if(r==true) {
// Do nothing
} else {
self.location = document.referrer;
}
// reset state
history.go(-state);
}
}, false);
}, false);
}
在这方面的任何帮助都将是非常可观的。
【问题讨论】:
标签: javascript jquery