Responsive Ad Area

Share This Post

test

IOS pagehide not triggered. Cannot detecting page unload

I call this function when my page is loaded:

function setBeacon() {
if (!navigator.sendBeacon) {
    alert("No navigator beacon available. This is a critical bug which must be fixed.");
}
else
{
    if ('beforeunload' in window) {
        $(window).on('beforeunload', function() {
            generateFormSendBeacon();
            return undefined;
        });
    } else if ('onpagehide' in window) {
        alert("on page hide");
        window.addEventListener("pagehide", function() {
            generateFormSendBeacon();
            return undefined;
        });
    } else {
        alert("Neither beforeunload or onpagehide events are available. Please report this to John, with details about your browser.");
    }
}

}

This works perfectly on desktop. I am using navigator.sendBeacon in the generateFormSendBeacon() function. It sends it correctly (I have a breakpoint in my server receiving this). On mobile however, in both safari and chrome, the ‘beforeunload’ event doesn’t exist. But according to apple, we should use pagehide instead. The ‘on page hide’ alert shows; iOS does have access to this event. However the event is never fired, as I never get to the breakpoint in my server. Again; testing the same code path on desktop, it works fine. How does one detect the page unloading on iOS?


IOS pagehide not triggered. Cannot detecting page unload
IOS pagehide not triggered. Cannot detecting page unload
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar