我如何检测用户是否正在从移动网络浏览器浏览我的网站,以便我可以自动检测和显示我的网站的适当版本?
当前回答
以下是我如何在JavaScript中做到这一点:
function isMobile() {
var index = navigator.appVersion.indexOf("Mobile");
return (index > -1);
}
在www.tablemaker.net/test/mobile.html上可以看到一个例子,它把手机上的字体放大了三倍。
其他回答
是user-agent用于检测手机浏览器。有很多免费的脚本可以检查这一点。这里有一个这样的php代码,它将帮助你重定向移动用户到不同的网站。
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.
您可以通过导航器简单地检测移动客户机。并根据检测到的客户端类型加载备用脚本,如下所示:
$(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
}
});
有一个全新的解决方案使用Zend框架。从Zend_HTTP_UserAgent的链接开始:
http://framework.zend.com/manual/en/zend.http.html
在检测移动浏览器上有开源脚本,可以在Apache、ASP、ColdFusion、JavaScript和PHP中实现这一点。
推荐文章
- HTML的“nonce”属性用于脚本和样式元素的目的是什么?
- 我如何在HTML中创建一个泪滴?
- 在另一个js文件中调用JavaScript函数
- 我怎么能强迫一个长字符串没有任何空白被包装?
- 在哪里放置JavaScript在HTML文件?
- 如何在引导栏中居中内容?
- IE8问题推特引导3
- 是否有可能使一个div 50px小于100%在CSS3?
- 为什么CSS选择器/ HTML属性首选破折号?
- 如何在标题属性中转义双引号
- Safari和Chrome桌面浏览器无法自动播放视频
- 自动高度
- 如何将JavaScript文件链接到HTML文件?
- 正确的mime类型的SVG图像嵌入字体
- jQuery的窗口大小调整