浏览器后退按钮单击确认框 [英] Browsers back button click with confirm box
本文介绍了浏览器后退按钮单击确认框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要创建JavaScript确认,然后单击浏览器的后退按钮弹出.如果我单击后退"按钮,则会弹出弹出窗口,并说您要继续吗?".如果单击是",则它将已重定向到上一页.
Need to create the javascript confirm pop up on click of browsers back button. If I click on back button pop up will come up and say "you want to go ahead ?" if click on Yes then it would have redirected to previous page.
我有以下代码,它不能按要求工作.
I have following code it is not working as per the requirement.
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);
}
在这方面的任何帮助都是非常有意义的.
Any help in this would be really appreciable.
推荐答案
尝试一下:这很简单,您可以完全控制后退"按钮.
Try this: it's simple and you get total control over the back button.
if (window.history && history.pushState) {
addEventListener('load', function() {
history.pushState(null, null, null); // creates new history entry with same URL
addEventListener('popstate', function() {
var stayOnPage = confirm("Would you like to save this draft?");
if (!stayOnPage) {
history.back()
} else {
history.pushState(null, null, null);
}
});
});
}
这篇关于浏览器后退按钮单击确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文