Yes / No in Javascript like this in StackOverflow?

I want to know how I can display a confirmation window when users try to exit this page, for example, here in StackOverflow, when you try to close the Ask a Question page.

Thank you in advance.

+3
source share
6 answers

Actually, all that is needed is the following:

window.onbeforeunload = function(){ return "You should save your stuff."; }

This is also a hoax: How to show "Are you sure you want to go from this page?" when did the changes happen?

+6
source

In javascript add a function to window.onbeforeunload, for example:

window.unbeforeunload = function (e)
{
    // return (prompt ('Are you sure you want to exit?'));

    // EDIT: Amended version below: 
    var e = e || window.event;
    if (e) e.returnValue = 'Your exit prompt';
    return 'Your exit prompt';
}

EDIT: , . .

: window.onbeforeunload(MDC)

+3

, : - JavaScript?, , . eventbeforeunload event

<script type="text/javascript">
   window.onbeforeunload = function() {
      //will show a dialog asking the user if 
      //they want to navigate away from the current page
      // ok will cause the navigation to continue, 
      // cancel will cause the user to remain on the current page
      return "Use this text to direct the user to take action.";
   }
</script>

: doh! , , OP: D

+2

onbeforeunload window. , , . .

+1
source

You can use the window.onbeforeunload event to catch this. If there is any value in the text box, a warning message is displayed.

+1
source

This browser is based.

As long as you implement the tag <form>, and you enter something on the form, and in Google Chrome, it will request this message.

-4
source

Source: https://habr.com/ru/post/1729021/


All Articles