Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In IE the onbeforeunload event is fired for links that don't unload the page

I am writing a plugin to a CMS (umbraco) and I wish to attach a warning dialog to various actions on the page, one such action is clicking a link (JavaScript links), in most browsers the following code works well:

$(".propertypane").delegate("a, a div", "click", function () { window.onbeforeunload = confirmNavigateAway; });

The following is an issue in IE because IE appears to trigger onbeforeunload event when any link is clicked, even though the link is not navigating away.

I've set up an example here: http://jsfiddle.net/DETTG/8/

Note: I do not have control over the ajax controls within the propertypane, they're written by third parties.

like image 954
Myster Avatar asked Oct 12 '11 02:10

Myster


People also ask

What is Onbeforeunload event?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.

How do you cancel Windows Onbeforeunload?

Cancelable: The beforeunload event can be canceled by user interaction: // by https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload#Example window. addEventListener("beforeunload", function(event) { event. preventDefault(); // Cancel the event as stated by the standard.


2 Answers

Maybe this page will help you?

like image 153
Yuriy Galanter Avatar answered Oct 01 '22 20:10

Yuriy Galanter


If you remove "href" then it will work. But then you would need to style it as a link element and add the attribute onclick if you want to execute a function. Here is the updated version: http://jsfiddle.net/DETTG/34/

<a onclick="alert('do some ajax');" style="color:blue; text-decoration:underline; cursor:pointer">javascript</a>
like image 32
Ernesto Avatar answered Oct 01 '22 20:10

Ernesto