[英]Browsers back button click with confirm box
Need to create the javascript confirm pop up on click of browsers back button.需要在点击浏览器后退按钮时创建 javascript 确认弹出窗口。 If I click on back button pop up will come up and say "you want to go ahead?"如果我点击后退按钮弹出窗口会出现并说“你想提前 go 吗?” 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);
}
});
});
}
/* Prevent accidental back navigation click */
history.pushState(null, document.title, location.href);
window.addEventListener('popstate', function (event)
{
const leavePage = confirm("you want to go ahead ?");
if (leavePage) {
history.back();
} else {
history.pushState(null, document.title, location.href);
}
});
The solution by Robert Moore caused duplicate events when refreshing the page for me. Robert Moore 的解决方案在为我刷新页面时导致重复事件。 Presumably since the state would be re-added multiple times.大概是因为状态会被重新添加多次。
I worked around it by only adding state if it is null.我通过仅在状态为空时添加状态来解决它。 I also clean up the listener before going back.我还在回去之前清理了听众。
window.onload = function () {
if (window.history && history.pushState) {
if (document.location.pathname === "/MyBackSensitivePath") {
if (history.state == null) {
history.pushState({'status': 'ongoing'}, null, null);
}
window.onpopstate = function(event) {
const endProgress = confirm("This will end your progress, are you sure you want to go back?");
if (endProgress) {
window.onpopstate = null;
history.back();
} else {
history.pushState(null, null, null);
}
};
}
}
};
MDN has a good read on managing state: https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method MDN 对管理状态有很好的了解: https : //developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method
It's possible to always display a confirmation box, when a user tries to leave the page.当用户试图离开页面时,始终显示一个确认框是可能的。 That also includes pressing the back button.这还包括按下后退按钮。 Maybe that's a suitable quick-fix for your issue?也许这是适合您的问题的快速解决方案?
window.addEventListener('beforeunload', function() {
return 'You really want to go ahead?';
});
http://jsfiddle.net/squarefoo/8SZBN/1/ http://jsfiddle.net/squarefoo/8SZBN/1/
This works for all scenarios, when这适用于所有场景,当
public componentDidMount(): void {
history.pushState(null, 'PageName', window.location.href)
window.onpopstate = (e: PopStateEvent) => {
if (confirm('Are you sure you want to leave?')) {
window.onpopstate = (e) => {
history.back()
}
history.back()
} else {
history.pushState(null, 'PageName', window.location.href)
}
}
}
public componentWillUnmount(): void {
window.onpopstate = null
}
Try This,试试这个,
window.onbeforeunload = function() {
return "You're leaving the site.";
};
$(document).ready(function() {
$('a[rel!=ext]').click(function() {
window.onbeforeunload = null;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.