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