Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent window.onbeforeunload from being triggered by javascript: href links in IE?

I'm building a fail safe for my form that is going to warn users that if they leave the page their form data will be lost (similar to what gmail does).

window.onbeforeunload = function () {             if (formIsDirty) {             return "You have unsaved data on this form. Don't leave!";         } } 

The above function works great in firefox, but in IE it is triggered by any href link, even ones that are links to javascript and not other pages.

for example....

<a href='javascript:someFunction();'>click</a> 

I'm wondering if there is any way to get around this, as I do not want the user thinking that they are leaving the page when they are simply clicking a button on it. I do not have the option of rewriting all the various links as they are built in and numerous.

Any ideas?

like image 426
Maxx Avatar asked Oct 04 '11 15:10

Maxx


People also ask

What triggers Onbeforeunload?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.

How do I cancel 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.

How do I stop navigation in JavaScript?

To prevent a webpage from navigating away using JavaScript, we can set the window. onbeforeunload property to a function that returns any value. window. onbeforeunload = () => { return ""; };

What is Beforeunload event in JavaScript?

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.

Does the onbeforeunload event work in Internet Explorer?

Note: This only works in Internet Explorer. Note: If the onbeforeunload event is not assigned to the <body> element, you must assign/attach the event on the window object, and use the returnValue property to create a custom message (see syntax examples below). The numbers in the table specify the first browser version that fully supports the event.

What is wrong with the beforeunload event in HTML?

The HTML specification states that calls to window.alert (), window.confirm (), and window.prompt () methods may be ignored during this event. See the HTML specification for more details. The beforeunload event suffers from the same problems as the unload event. Especially on mobile, the beforeunload event is not reliably fired.

When the page is about to be unloaded in JavaScript?

Execute a JavaScript when the page is about to be unloaded: Definition and Usage. The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page.

What if the onbeforeunload event is not assigned to the <body> element?

Note: If the onbeforeunload event is not assigned to the <body> element, you must assign/attach the event on the window object, and use the returnValue property to create a custom message (see syntax examples below). The numbers in the table specify the first browser version that fully supports the event.


1 Answers

You may remove and re-assign the onbeforeunload when hovering those links:

jQuery(   function($)   {       //store onbeforeunload for later use     $(window).data('beforeunload',window.onbeforeunload);          //remove||re-assign onbeforeunload on hover      $('a[href^="javascript:"]')       .hover(               function(){window.onbeforeunload=null;},              function(){window.onbeforeunload=$(window).data('beforeunload');}             );    } ); 
like image 191
Dr.Molle Avatar answered Oct 09 '22 16:10

Dr.Molle