mailto link (in chrome) is triggering window.onbeforeunload - can i prevent this?
Add a flag and see if it is flipped, set the flag on the link click.
var ignore = false
window.onbeforeunload = function() {
if (changed && !ignore) {
return "You have unsaved changes. Do you really want to leave this page without saving?";
} else {
ignore = false;
}
}
And the link
<a class="button button-alt" href="mailto:[email protected]" onclick="ignore=true">Report a problem</a>
It would be better to add the onclick with JavaScript code.
Building off of epascarello's solution, the following JQuery code should do the trick:
var ignore_onbeforeunload = false;
$('a[href^=mailto]').on('click',function(){
ignore_onbeforeunload = true;
});
window.onbeforeunload = function() {
if (!ignore_onbeforeunload){
return "Halt! you are not supposed to leave!";
}
ignore_onbeforeunload = false;
};
A really simple fix to this is to do something like this:
<a href="mailto:[email protected]" target="hidden-iframe">Email me</a>
<iframe name="hidden-iframe" style="visibility:hidden;position:absolute;"></iframe>
(And of course, move the styles to their own stylesheet instead of inlining them.)