使用“桌面模式”浏览器仅检测移动用户

时间:2013-06-27 13:32:06

标签: php jquery-mobile mobile user-experience

我知道有很多方法可以检测移动用户(主要通过检查用户代理)。

但是许多移动浏览器都有所谓的“桌面模式”,它为网站提供了更多功能的环境。

有没有办法只为这些移动用户提供特定功能(例如jQuery滑块),在这种模式下浏览?我遇到的真正问题是,基本上,他们的用户代理在两种模式下都是相同的(例如“Opera Mini 9.0.1”),所以从网站管理员的角度来看 - 我怎么知道他们在移动设备上但是以桌面模式浏览网站?

2 个答案:

答案 0 :(得分:2)

在Android Chrome上,"桌面模式"移除" Android"用户代理的字符串。如果您可以使用JavaScript,则以下 主要检测Android Chrome桌面模式:

var webkitVer = parseInt(/WebKit\/([0-9]+)|$/.exec(navigator.appVersion)[1], 10); // also matches AppleWebKit
var isGoogle = webkitVer && navigator.vendor.indexOf('Google') === 0;  // Also true for Opera Mobile and maybe others
var isAndroid = isGoogle && userAgent.indexOf('Android');  // Careful - Firefox and Windows Mobile also have Android in user agent
var androidDesktopMode = !isAndroid && isGoogle && (navigator.platform.indexOf('Linux a') === 0) && 'ontouchstart' in document.documentElement;

假设使用ARM处理器的Chrome是Android。对于在ARM上运行Linux的用户来说,这个假设肯定是失败的,对i686或MIPS等上的Android失败了(我还没有能够测试ChromeOS)。

对于Windows Mobile,您可以通过检查字符串" WPDesktop;"来检测桌面模式。在用户代理中。

编辑:用于使用window.chromewindow.chrome.webstore这是一项可靠测试的代码,但在Chrome 65周围,您无法再使用这些属性来检测桌面模式。感谢@faks的信息。

答案 1 :(得分:2)

这是iOS Safari用户的相关代码。本质上,用户代理在桌面模式下会丢失对iPhone / iPod / iPad的引用,但是该信息仍存在于navigator.platform中:

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);
相关问题