window.onbeforeunload url exception

Posted on 2009-04-26
Last Modified: 2013-11-08
Hi everyone,

I have a page which shows a confirm message when the user tries to leave it, using window.onbeforeunload.

Now I only want it to fire if the url the user is going to is not on my domain. i.e. he'll only get it when trying to close the tab/window or surfing to a different website, but not when reloading my page.

Question by:Feivi99
    LVL 12

    Expert Comment

    I dont think the javascript knows the URL of where it is going to. All I can think of is to use a javascript variable to track if it is ok to leave or not.

    var isOkToLeave = false;

    function MyBeforeUnloadEvent()
        if (!isOkToLeave )
               return confirm('do you really want to leave?');

    then in all your code for every button or hyperlink have some client event that sets this to true.

    <asp:Button .... OnClientClick="isOkToLeave=true;" .. />

    This way you are telling the onbeforeclose event that it is ok to leave, otherwise hitting the back button on the browser, typing a new URL, etc, will cause the confirm dialog to be presented

    Author Comment

    wht, that worked pretty nicely. The only thing that's missing for me is to keep it from happening when the user clicks the browser's "reload" button. Any way you can think of doing that?
    LVL 12

    Assisted Solution

    sorry to take so long to get back to you. (2) things:

    (1) obviously I'm sure you realized my error above  the function should have been something like

     function MyBeforeUnloadEvent() {
        if (!isOkToLeave)
            return 'Any unsave changes will be discarded.';

    Guess I spazzed out above with an extra confirm message in the function. no need for that because return a string force the browser to prompt the user as it is

    (2) unfortunetly i know of no way to tell a reload request (f5) apart other toolbar based actions that would cause the onbeforeunload event to trigger
    LVL 12

    Expert Comment

    since that worked ok for you above, could you accept the solution please. Thanks - KW

    Accepted Solution

    Sorry for the delay.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
    In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now