Saturday 19th July 2008

by Mr Me

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)

Tags: , , , , , , , ,

· · · ◊ ◊ ◊ · · ·

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

  1. NG says:

    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.

  2. Rax says:

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

  3. gul says:

    really great work thank u for advices

  4. NG says:

    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.

  5. Thanks! That’s very useful.

  6. jeff says:

    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.

  7. Naidu says:

    hi,

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

    Thanks,
    Naidu
    vrsanaidu@gmail.com

  8. iç giyim says:

    thank you in advance.

  9. Hassan says:

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

  10. Kaushik says:

    Great works for me!!

  11. Christopher says:

    Thanks, nice stuff.

  12. Emre says:

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

  13. Took says:

    That what i want. Thank you very much.

  14. rich says:

    Thanks! Simple, works.

  15. D.Asuncion says:

    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.

  16. An Nguyen says:

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

  17. Warning before navigate away from a page…

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

· · · ◊ ◊ ◊ · · ·

Leave a Reply

Need your support
More in html (15 of 33 articles)


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