Link to home
Start Free TrialLog in
Avatar of Feivi99
Feivi99

asked on

window.onbeforeunload url exception

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.

TIA!
Avatar of wht1986
wht1986
Flag of United States of America image

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
Avatar of Feivi99
Feivi99

ASKER

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?
SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Feivi99,
since that worked ok for you above, could you accept the solution please. Thanks - KW
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial