TimeMe.js icon indicating copy to clipboard operation
TimeMe.js copied to clipboard

onbeforeunload not supported in iOS

Open kalimerre opened this issue 8 years ago • 3 comments

Hello,

I have tested to send a time to database with AJAX script with the event onbeforeunload and it seems to not working. Someone has the same issue ?

I tried with onunload and it seems to work, any problem to use this function instead onbeforeunload ?

Regards

kalimerre avatar Jan 05 '17 16:01 kalimerre

Hi! Thanks for the comment. There are timing issues with onbeforeunload, especially when trying to send a synchronous HTTP request as my documentation suggests to do. I'm not surprised to hear that iOS is not supporting this.

I'll update documentation to find another suggestion, but it may involve websockets or some other pinging mechanism.

jasonzissman avatar Jan 28 '17 00:01 jasonzissman

I've been play with TimeMe and Ratchet and have it all working nicely, except iOS. Is this due to the sendCurrentTime call that should be triggered by an onbeforeunload that never runs on iOS?

martyf avatar Feb 16 '17 02:02 martyf

This is what I use for iPhone iPad and iPod. It seems to work if someone wants to test and give me feedback (and maybe update documentation)

<script type="text/javascript">
    TimeMe.setIdleDurationInSeconds(30);
    TimeMe.setCurrentPageName("NAME_OF_THE_PAGE");
    TimeMe.initialize();
    var isOnIOS = navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i);
    var eventName = isOnIOS ? "pagehide" : "beforeunload";

    window.addEventListener(eventName, function (event) { 
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", 'URL', false);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
        xmlhttp.send("timeSpent=" + timeSpentOnPage);
    });

</script>

kalimerre avatar Feb 19 '17 12:02 kalimerre