Prevent onbeforeunload from calling when clicking on the mailto link

In any case, to prevent onbeforeunload from being called when you click on the mailto link in chrome. In FF, Safari, IE is working fine.

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
        google.load("jquery", "1.3.2");
    </script>

    <script type="text/javascript">
        $(document).ready(function(){
            window.onbeforeunload = confirmExit;
        });

        function confirmExit() {
            return "Are you sure?";
        }
    </script>
</head>
<body>
    <a href="mailto:someone@somewhere.com?subject=test mail&body=Hello%20World">Mail Link</a>
</body>
</html>
+3
source share
2 answers

How about a workaround?

$(document).ready(function(){
    mailtoClicked = false;
    window.onbeforeunload = confirmExit;
    //Test if browser is Chrome
    if (/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())) {
        $('a[href^=mailto]').click(function() {mailtoClicked = true;});
    }
});

function confirmExit() {
    if (!mailtoClicked) {
        return "Are you sure?";
    } else {
        mailtoClicked = false;
    }
}

Demo .

+5
source

Here are suggested solutions that look reasonable

http://www.ilovebonnie.net/2009/09/23/how-to-use-onbeforeunload-with-form-submit-buttons/


UPDATE (link dead) - copied content from Google Cache

How to use onbeforeunload using form submit buttons

September 23, 2009 - Geekery

. , , , , ( ) onbeforeunload.

, AJAX/JavaScript, , (, IE , , - JavaScript). , , , , .

:

window.onbeforeunload = function () {
    return "You are about to leave this order form. You will lose any information...";
}

, , , , , , . , , , :

// Create a variable that can be set upon form submission
var submitFormOkay = false;
window.onbeforeunload = function () {
    if (!submitFormOkay) {
        return "You are about to leave this order form. You will lose any information...";
    }
}

onclick, , onsubmit submit, , submitFormOkay true , :

<input type="submit" id="submit_button" onclick="submitFormOkay = true;">
+1

Source: https://habr.com/ru/post/1723110/


All Articles