Mobile Device Detection

987 views
Skip to first unread message

Alice CHING

unread,
Feb 22, 2022, 12:03:15 AM2/22/22
to oTree help & discussion
Dear all,

I'm running an online experiment where I need participants to use only laptop/desktop but not mobile devices.

Thanks to otree developers, there's a code snippet that helps detect mobile browsers. However, I noticed that in that code snippet,  the way to detect iPad is to find string "iPad" in the user-agent, yet after iOS 13, iPad's user-agent includes 'Macintosh' instead of 'iPad', which is the same as Mac. In other words, we can no longer tell iPad from Mac based on user-agent alone.

I was wondering how you guys detect iPad using JavaScript. Any help is appreciated :)

Best regards,
Ningning

Alice CHING

unread,
Feb 22, 2022, 12:05:56 AM2/22/22
to oTree help & discussion
I've looked it up in StackOverflow, and is currently trying out this workaround:

if (navigator.userAgent.match(/Mac/) && navigator.maxTouchPoints){
    // if the device is an iPad
    return true
}

However, the answer is 3 years ago and I'm not sure how it works nowadays.
Reply all
Reply to author
Forward
0 new messages