0

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>

2 Answers 2

3

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

1
  • 1
    That's what I expected, but hope springs eternal. Mar 7, 2011 at 8:57
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.