Prompting and preventing user from navigating away/closing a page

后端 未结 1 1789
你的背包
你的背包 2021-01-13 18:15

As per how StackOverflow does it when you are adding a question or replying to a question;

I\'d like to prompt the user with a;

\"Are you sure you wa         


        
相关标签:
1条回答
  • 2021-01-13 18:41

    You need to implement window.onbeforeunload and return your message from it as a string:

    window.onbeforeunload = function() {
        if (theUserHasStartedEditing) {
            return "You have started writing or editing a post."
        }
    }
    

    There's an online demo here: http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo2.htm

    0 讨论(0)
提交回复
热议问题