我如何检测用户是否正在从移动网络浏览器浏览我的网站,以便我可以自动检测和显示我的网站的适当版本?
当前回答
MobileESP有PHP, Java, APS。NET (c#), Ruby和JavaScript钩子。 它还拥有Apache 2许可证,因此可以免费用于商业用途。 对我来说,关键是它只识别浏览器和平台,而不识别屏幕大小和其他指标,这使它保持较小的规模。
其他回答
是user-agent用于检测手机浏览器。有很多免费的脚本可以检查这一点。这里有一个这样的php代码,它将帮助你重定向移动用户到不同的网站。
以下是我如何在JavaScript中做到这一点:
function isMobile() {
var index = navigator.appVersion.indexOf("Mobile");
return (index > -1);
}
在www.tablemaker.net/test/mobile.html上可以看到一个例子,它把手机上的字体放大了三倍。
是的,读取User-Agent报头就可以了。
有一些已知的移动用户代理列表,所以您不需要从头开始。当我不得不这样做时,我所做的是建立一个已知用户代理的数据库,并在检测到需要修改的未知数据时存储它们,然后手动找出它们是什么。在某些情况下,最后一项可能太过了。
如果希望在Apache级别上执行该操作,可以创建一个脚本,定期生成一组重写规则,检查用户代理(或者只检查一次,不考虑新的用户代理,或者每月检查一次,取决于您的情况),例如
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (OneMobileUserAgent|AnotherMobileUserAgent|...)
RewriteRule (.*) mobile/$1
例如,将请求移到http://domain/index.html到http://domain/mobile/index.html
如果你不喜欢让脚本定期重新创建htaccess文件的方法,你可以写一个模块来检查用户代理(我没有找到一个已经创建的,但找到了这个特别合适的例子),并从一些站点获取用户代理来更新它们。然后你可以把这个方法尽可能复杂化,但我认为在你的情况下,之前的方法就可以了。
您可以通过导航器简单地检测移动客户机。并根据检测到的客户端类型加载备用脚本,如下所示:
$(document).ready(function(e) {
if(navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)) {
//write code for your mobile clients here.
var jsScript = document.createElement("script");
jsScript.setAttribute("type", "text/javascript");
jsScript.setAttribute("src", "js/alternate_js_file.js");
document.getElementsByTagName("head")[0].appendChild(jsScript );
var cssScript = document.createElement("link");
cssScript.setAttribute("rel", "stylesheet");
cssScript.setAttribute("type", "text/css");
cssScript.setAttribute("href", "css/alternate_css_file.css");
document.getElementsByTagName("head")[0].appendChild(cssScript);
}
else{
// write code for your desktop clients here
}
});
Just a thought but what if you worked this problem from the opposite direction? Rather than determining which browsers are mobile why not determine which browsers are not? Then code your site to default to the mobile version and redirect to the standard version. There are two basic possibilities when looking at a mobile browser. Either it has javascript support or it doesn't. So if the browser does not have javascript support it will default to the mobile version. If it does have JavaScript support, check the screen size. Anything below a certain size will likely also be a mobile browser. Anything larger will get redirected to your standard layout. Then all you need to do is determine if the user with JavaScript disabled is mobile or not. According to the W3C the number of users with JavaScript disabled was about 5% and of those users most have turned it off which implies that they actually know what they are doing with a browser. Are they a large part of your audience? If not then don't worry about them. If so, whats the worst case scenario? You have those users browsing the mobile version of your site, and that's a good thing.