Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onbeforeunload event is too enthusiastic in IE9

Here's some sample test html:

<!DOCTYPE html>
<html>
  <body>
    <a href="javascript:alert('Not going anywhere!');">Go nowhere 1</a>
    <a href="javascript:void(0);" onclick="alert('Not going anywhere!');">Go nowhere 2</a>
    <a href="http://www.google.com">Go somewhere</a>
    <script type="text/javascript">
      window.onbeforeunload = function() { return "Really leave?"; };
    </script>
  </body>
</html>

This is available as a working page here: timjeanes.com/ie9_onbeforeunload.htm

In Firefox, Chrome and Safari, I get the behaviour I want. That is, clicking either of the first two links shows an alert dialog; clicking the third link (or otherwise navigating away) shows the "Are you sure you want to leave this page?" message.

However, in IE9, the "Are you sure you want to leave this page?" message also shows when you click either of the first two links, even though no navigation is taking place - we're just running some javascript.

Is there something I'm doing wrong? Or is there a nice workaround for IE?

One option would be to use href="#" and put my javascript in the onclick. I'm not keen on that as it takes the user to the top of the page, and my real-life page is quite tall.

I've not tested in other versions of IE.

like image 264
teedyay Avatar asked Aug 31 '11 20:08

teedyay


5 Answers

That's a pretty unfortunate bug. It seems you'll have to work around it, and using the hash link method is probably the best way. To avoid taking the user to the top of the page, cancel the event using event.preventDefault() and event.returnValue = false.

function myClickHandler(e) {
    if (e.preventDefault)
        e.preventDefault();
    e.returnValue = false;
    alert("Not going anywhere!");
}

Call it like this:

<a href="#" onclick="myClickHandler(event)">Go nowhere 1</a>

Edit: You could cancel the event for all the hash-links on your page like this:

var links = document.links;
for (var i = 0; i < links.length; i++) {
    var link = links[i];
    if (link.href == "#") {
        if (link.addEventListener) {
            link.addEventListener("click", cancelEvent, false);
        }
        else if (link.attachEvent) {
            link.attachEvent("onclick", cancelEvent);
        }
    }
}
function cancelEvent(e) {
    e = e || window.event;
    if (e.preventDefault)
        e.preventDefault();
    e.returnValue = false;
}

Or with jQuery using just one line of code:

$("a[href='#']").click(function (e) { e.preventDefault(); });
like image 189
gilly3 Avatar answered Oct 24 '22 05:10

gilly3


A simpler solution is to use onunload in IE and onbeforeunload in other browsers.

like image 33
CpnCrunch Avatar answered Oct 24 '22 04:10

CpnCrunch


#null in href to prevent page jumping rather than just # and script in onclick=""

like image 23
Stuart Green Avatar answered Oct 24 '22 06:10

Stuart Green


You should not bind an event for click using href="javascript:...: Below is not good.

<a href="javascript:alert('Not going anywhere!');">Go nowhere 1</a>

If your going to use an onclick and void the href then just return false in the onclick.

<a onclick="alert('Not going anywhere!');return false;">Go nowhere 2</a>
like image 2
John Hartsock Avatar answered Oct 24 '22 05:10

John Hartsock


It's actually not a bug that the "Are you sure you want to leave this page?" message pops up when you click the first links; it seems to be a "feature" in IE 9 - More reasons I dislike IE

like image 1
clarkb86 Avatar answered Oct 24 '22 04:10

clarkb86