Saturday 19th July 2008

by Sajith M.R

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: , , , , , , , ,

· · · ◊ ◊ ◊ · · ·

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

  1. 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.

  2. Naidu says:

    hi,

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

    Thanks,
    Naidu
    vrsanaidu@gmail.com

  3. iç giyim says:

    thank you in advance.

  4. Hassan says:

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

  5. Kaushik says:

    Great works for me!!

  6. Christopher says:

    Thanks, nice stuff.

  7. Emre says:

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

  8. Took says:

    That what i want. Thank you very much.

  9. rich says:

    Thanks! Simple, works.

  10. 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.

  11. An Nguyen says:

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

  12. Warning before navigate away from a page…

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

· · · ◊ ◊ ◊ · · ·

Leave a Reply

وظائف 2011 تحويل العملات برامج برنامج تسريع التحميل برنامج الفلاش برنامج محول الصوتيات عربي hotel 2011 زيادة رواتب العساكر 1431