简体   繁体   English

浏览器后退按钮单击确认框

[英]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);
    }  
});
window.onbeforeunload = function() {
    return "Leaving this page will reset the wizard";
};

This would help you.这会帮助你。

DEMO演示

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这适用于所有场景,当

  1. user click on back button and cancel the confirmation.用户单击后退按钮并取消确认。
  2. user clicks again on back button and cancel the confirmation.用户再次点击后退按钮并取消确认。
  3. user clicks again on back button and click ok.用户再次单击后退按钮并单击确定。
  4. so on...很快...
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.

相关问题 浏览器后退按钮上的确认框 - Confirm box on browser back button 无法在带有JavaScript的确认框中单击按钮 - Not able to click Button on a confirm box with javascript 确认是否在卸载/退出对话框上,是否按下了后退按钮? - Confirm dialog box on unload/exit UNLESS the back button is pressed? 在按钮上单击确认警告框,查询提交也取消 - on button click with confirm alert box, query submitting on cancel also 在使用滑轨4单击按钮时显示确认对话框? - Display confirm dialog box on button click using rails 4? 单击确认消息框中的取消按钮后如何清除文本框? - How to clear textbox after click of cancel button in confirm message box? Javascript - 确认框以在用户单击退出按钮时重定向用户 - Javascript - Confirm box to redirect user when they click on exit button 带有确认的Java后退按钮 - Javascript back button with confirm 为什么当我单击页面时,浏览器的确认框(由beforeunload事件处理程序触发)才出现? - Why the browsers confirm box (triggered by beforeunload event handler) only appears when I click onto the page? 在确认框中单击确认或取消按钮后,如何使用JavaScript或jQuery返回true或false值? - How to return true or false values using JavaScript or jQuery after click on confirm or cancel button from confirm box?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM