Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
156
rated 0 times [  157] [ 1]  / answers: 1 / hits: 29741  / 13 Years ago, tue, october 4, 2011, 12:00:00

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?


More From » jquery

 Answers
92

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');}
);

}
);

[#89798] Sunday, October 2, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
briannar

Total Points: 354
Total Questions: 103
Total Answers: 101

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
;