在浏览器中检测用户是在桌面上还是在移动设备上

问题描述 投票:0回答:1

我有一个网页,正在尝试检测用户是在桌面上还是在移动设备上访问该页面。执行此操作的典型方法是什么?注意我不想想要使用页面宽度(例如window.innerWidth<= 767) to check as the user may simply have resized the desktop. Can someone assist in the best way to achieve this?

谢谢!

javascript browser user-agent
1个回答
0
投票

为此目的规划的机制是 HTTP 标头中的 User-Agent 字段。 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent 但考虑一下 https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent 您可以在其中找到用于测试特定功能的 JavaScript 示例。

如果您想与客户进行测试,请查看 https://whatmyuseragent.com/

你好,xg

© www.soinside.com 2019 - 2024. All rights reserved.