Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onbeforeunload not supported in iOS #28

Open
kalimerre opened this issue Jan 5, 2017 · 3 comments
Open

onbeforeunload not supported in iOS #28

kalimerre opened this issue Jan 5, 2017 · 3 comments

Comments

@kalimerre
Copy link

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

@jasonzissman
Copy link
Owner

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.

@martyf
Copy link

martyf commented Feb 16, 2017

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?

@kalimerre
Copy link
Author

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants