When I close my page on some browser I want a message box to appear and to ask me if I really want to close the page or not, with two buttons and if I click No then this tab won’t be closed.
How can I do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to listen on the
beforeunloadevent.Here’s a kickoff example:
This message will show up in kind of a confirmation dialogue. This message will show up right before the client unloads the page. That can be a browser close, but that can also be a simple navigational action like clicking a link or submitting a form in the page!
You would most probably also like to turn it off (just set to
null) whenever an internal link is clicked or an internal form is submitted. You namely don’t want to annoy endusers with unintuitive behaviour. You can do that by listening on theclickevent of the desired links and thesubmitevent of the desired forms. jQuery may be of great help here since it does that in crossbrowsercompatible way so that you don’t need to write >20 lines of JS code for this:You only need to give all external links the defacto standard attribute
rel="ext"to denote that those are external links.