Charles Anderson
Charles Anderson

Reputation: 20039

Cancelling in beforeunload event handler leaves browser URL changed

I am using the beforeunload event to get users to confirm that they want to leave my web page (and lose their input). It works fine except for one case: if they try to navigate away by typing a new URL into the address bar, and then cancel, they remain on my page but the address bar still shows their new destination.

This is harmless but annoying, so I am looking for a way to set the address bar back to my URL, without causing a refresh.

This is my code:

<script type="text/javascript">jQuery(window).bind('beforeunload', function () {
    return 'This will end your session.';
});
</script>

Upvotes: 0

Views: 490

Answers (2)

mwilcox
mwilcox

Reputation: 4132

I even tried adding an anchor to the page:

<a href="#foo">Change Location</a>

Clicking that does not even fix a deleted address. I would think this verifies you can't do it.

Upvotes: 2

ithcy
ithcy

Reputation: 5589

You can't programatically change the content of the address bar unless you actually send the user to a new page. If you could, phishing would be much, much easier. "Hmm, the browser says I'm at https://safebankingsite.com, I guess it's OK to enter my SSN."

Upvotes: 3

Related Questions