Warning before navigate away from a page

In some cases, if you are in between any work of editing or typing a content, and you wont let the visitor to navigate away from the page without particular condition, say the document is not saved yet, you can block the navigation (even the closing of the browser window) with Javascript.

Here is the javascript code for that:


<script>

window.onbeforeunload = function (evt) {
var message = ‘Are you sure you want to leave?’;
if (typeof evt == ‘undefined’) {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}

</script>

Just paste this code anywhere in your html page. The condition settings , warning messages are up to you. In the above code, the warning will be displayed when ever a navigation happen (Even closing of browser window happen)

(pic from Binney V.A)

19 Comments , , , , , , , ,

19 Responses to “Warning before navigate away from a page”

  1. Binny V A July 19, 2008 at 11:07 pm #

    I wrote an article about this a while ago – Confirmation on Leaving the Current Page in JavaScript

    • Sushant October 3, 2012 at 4:31 pm #

      Thank you very much Binny….

      But It doesn’t work in chrome. Please help me on this.

      Thanks

  2. An Nguyen January 6, 2009 at 2:37 pm #

    This solution help me resolved my task which I take many time to find.
    Thank you very!

  3. D.Asuncion March 12, 2009 at 7:33 am #

    Olahhhh… Nice Idea..

    Good Day, i have a question in debugging, how can i detect in code behind when i click “Ok”?

    Thanks in Advance.

  4. rich March 25, 2009 at 3:07 am #

    Thanks! Simple, works.

  5. Took April 2, 2009 at 9:45 am #

    That what i want. Thank you very much.

  6. Emre April 16, 2009 at 8:30 pm #

    Hi ,
    I need to differentiate window close and redirect to another link. Is there a way for this ?

  7. Christopher June 12, 2009 at 12:15 pm #

    Thanks, nice stuff.

  8. Kaushik August 6, 2009 at 12:21 am #

    Great works for me!!

  9. Hassan August 22, 2009 at 3:37 am #

    This code is not working for me. Please help? I pasted this code under head tag.

  10. iç giyim August 24, 2009 at 12:26 pm #

    thank you in advance.

  11. Naidu August 29, 2009 at 4:48 pm #

    hi,

    is there any way to remove this custom message completely. please mail me solution,.

    Thanks,
    Naidu
    vrsanaidu@gmail.com

  12. jeff December 12, 2009 at 7:09 pm #

    is there a way this script could be modified to only warn someone when they are navigating away from MY website. If they’re navigating to another link on my site, then do not generate a warning.

  13. David Hardisty February 3, 2011 at 6:58 am #

    Thanks! That’s very useful.

  14. NG March 10, 2011 at 11:10 pm #

    While beneficial when working on documents or similar programing on your private network or computer. this type of software when browsing the Internet is often percieved as browser hijacking requiring the visitor to install extra security, blocking and script to browser to prevent it. In many cases most people will simply close their browser or worst case reboot to escape. It’s simply rude to try to keep visitors in the hopes they’ll click an ad. However it’ is useful in saving work done on a private computer. Which is the only place it should ever be found.

  15. gul July 8, 2011 at 4:33 pm #

    really great work thank u for advices

  16. Rax October 6, 2011 at 1:18 am #

    I want to give warning only when there is unsaved data how can i do this ..?

  17. NG December 10, 2011 at 1:17 pm #

    Just an additional note for those trying to escape a rude webmasters use of this. In Firefox hit tools, options and click the Java script box. Similar means are available for other browsers. Again to reiterate. “Are you sure you want to go somewhere else?” should only be on a private or secure network and used solely for form purposes, not to hijack a visitor to stay on your online website.

Trackbacks/Pingbacks

  1. www.blipoo.com - August 20, 2008

    Warning before navigate away from a page…

    How can we prevent the navigation away from the page while doing another job…

Leave a Reply

More in html (15 of 33 articles)


What you add for showing google ads on your website ? It is a javascript , right ? Parameters ...