admin管理员组

文章数量:1325236

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 want to navigate away from this page"
"Press OK to continue or Cancel to stay on this page"

I've tried a couple of js snippets i've found on ze internet, but nothing quite cuts the mustard.

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 want to navigate away from this page"
"Press OK to continue or Cancel to stay on this page"

I've tried a couple of js snippets i've found on ze internet, but nothing quite cuts the mustard.

Share Improve this question asked Apr 14, 2010 at 15:59 GordonByGordonBy 3,4076 gold badges33 silver badges53 bronze badges 2
  • 4 More detail on the mustard please. – D'Arcy Rittich Commented Apr 14, 2010 at 16:02
  • 1 possible duplicate of stackoverflow./questions/1289234/… – Peter Bailey Commented Apr 14, 2010 at 16:06
Add a ment  | 

1 Answer 1

Reset to default 7

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: https://web.archive/web/20210619174356/https://www.4guysfromrolla./demos/OnBeforeUnloadDemo2.htm

本文标签: javascriptPrompting and preventing user from navigating awayclosing a pageStack Overflow