admin管理员组文章数量:1225102
I'd like to send iPad version of my web site when users use "request desktop site" of iOS mobile safari or "request desktop version" of iOS Chrome. It seems that only user-agent is different in that mode, and it seems impossible to detect. Any ideas?
My site has three versions : desktop / tablet / smartphone. The tablet version is a static version of desktop version which is very dynamic and uses JavaScript heavily (parallax effects.)
I'd like to send iPad version of my web site when users use "request desktop site" of iOS mobile safari or "request desktop version" of iOS Chrome. It seems that only user-agent is different in that mode, and it seems impossible to detect. Any ideas?
My site has three versions : desktop / tablet / smartphone. The tablet version is a static version of desktop version which is very dynamic and uses JavaScript heavily (parallax effects.)
Share Improve this question asked May 15, 2015 at 4:41 apptaroapptaro 2891 gold badge2 silver badges9 bronze badges3 Answers
Reset to default 4Please see the answer to this question: https://stackoverflow.com/a/58064481/1237536
it works flawlessly (for now, anyway)..
Basically, when iPads are in 'Desktop mode', they advertise themselves as MacIntel, but no Mac currently has a touchscreen, so you look for a MacIntel with touch enabled:
let isIOS = /iPad|iPhone|iPod/.test(navigator.platform) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
but you should definitely look at the original answer -- i'm not trying to take credit: https://stackoverflow.com/a/58064481/1237536
I've seen a few people using cookies/sessionStorage to detect the changing user agent, such as http://leavesofcode.net/2013/07/08/responsive-design-with-switch-to-desktop-site-option/ and https://gist.github.com/dtipson/7401026
However, I found you can skip all that, by cross-referencing navigator.platform
against navigator.userAgent
. This is for iOS specifically, as I hear Chrome's "request desktop site" also updates the viewport automatically so this check may not be required on it.
var iOSAgent = window.navigator.userAgent.match(/iPhone|iPod|iPad/);
var iOSPlatform = window.navigator.platform && window.navigator.platform.match(/iPhone|iPod|iPad/);
var iOSRequestDesktop = (!iOSAgent && iOSPlatform);
Another dirty hack is to use platform.maxTouchPoints
on iOS 13+. This field is still present even after requesting the desktop version, but on real desktop Safari this field is absent.
版权声明:本文标题:javascript - How to detect "request desktop site" mode of iOS Mobile Safari and Chrome? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739389995a2161123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论