简体   繁体   English

有没有检测移动浏览器的好方法?

[英]Is there an elegant way to detect mobile browsers?

I have a website that, when accessed by a mobile browser, to redirect to a different page . 我有一个网站 ,通过移动浏览器访问该网站时,它可以重定向到其他页面

I have been having trouble coming up with a way that reliably detects mobile devices. 我一直想出一种可靠地检测移动设备的方法。 Currently, I Javascript code like this: 目前,我的Javascript代码如下:

if (document.documentElement.clientWidth < 680 || 
    document.documentElement.clientHeight < 450) 
  location.href = "handheld.aspx";

that redirects if the broswer size is smaller than what is supported on the desktop version. 如果浏览器的大小小于桌面版本支持的大小,则会重定向。 The problem with this is that some mobile browsers start zoomed way out and have the similar browser dimensions as desktop devices. 问题是某些移动浏览器开始缩小,并且具有与台式设备相似的浏览器尺寸。

I've researched this some, and I've mainly found solutions that involve sniffing the user agent sting and checking it against a bunch of strings in existing mobile devices. 我已经对此进行了研究,并且主要找到了一些解决方案,这些方案涉及嗅探用户代理字符串并根据现有移动设备中的一串字符串对其进行检查。 If possible, I'd like to avoid that route because it seems extremely messy and will break in the future when new devices come out. 如果可能的话,我想避免使用该路由,因为它看起来非常凌乱 ,将来会在新设备问世时中断。

Is there a less kludgy way to detect mobile browsers, or at least a more reliable way to detect screen size? 有没有一种比较灵活的方法来检测移动浏览器,或者至少有一种更可靠的方法来检测屏幕大小?

You have to decide which aspect of a mobile browser you are most trying to detect. 您必须确定最想检测移动浏览器的哪个方面。 Are you trying to detect the screen size and redirect to a site design that fits on a smaller screen. 您是否要检测屏幕尺寸并重定向到适合较小屏幕的网站设计。 If so, you can use window.screen ( doc here ). 如果是这样,您可以使用window.screen (在doc此处 )。

If you're trying to detect specific capabilities or lack of capabilities in mobile browsers, then you should use feature detection to find out whether the specified features are there or not there. 如果要检测移动浏览器中的特定功能或功能不足,则应使用功能检测来发现指定的功能是否存在。 This is way better than detecting browser user agents. 这比检测浏览器用户代理更好。

Detecting based on screen size is a losing battle in a world where new screen sizes are popping up left and right. 在屏幕左右左右弹出新世界的世界中,基于屏幕大小进行检测是一场失败的战斗。 You should use server-side detection and modify your view accordingly. 您应该使用服务器端检测并相应地修改视图。 Using a service like Device Atlas or WURFL will future-proof you and give you far more capabilities that you're likely to accomplish on your own. 使用Device Atlas或WURFL之类的服务将使您适应未来的发展,并为您提供更多可能自己完成的功能。

For the record, JS-based mobile redirects are the worst-possible solution from a performance standpoint and should be avoided. 出于记录,从性能角度来看,基于JS的移动重定向是最糟糕的解决方案,应避免使用。

否。您无法检查屏幕尺寸或用户代理

If you want an easy fix for this, you can try handsetdetection javascript It gives you the ability to set rules and conditions such as screen size, vendor, OS, Browser, display width and height for your redirection. 如果您想要一个简单的解决方法,则可以尝试手机检测javascript,它使您能够设置规则和条件,例如屏幕大小,供应商,操作系统,浏览器,重定向的显示宽度和高度。 And if you are fond of statistics, HD has a great analytics reporting that you can use to fine tune how your redirection is served to your site users/visitors. 而且,如果您喜欢统计数据,HD会提供出色的分析报告,您可以使用它来微调如何将重定向提供给站点用户/访问者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM