自动检测移动浏览器(通过用户代理?)

本文探讨了如何通过用户代理检测移动Web浏览器,提供了一种使用PHP代码重定向移动用户至特定页面的方法,并讨论了在Apache级别实现这一功能的策略。此外,还介绍了通过JavaScript检测移动设备并加载相应脚本的技术。
摘要由CSDN通过智能技术生成

本文翻译自:Auto detect mobile browser (via user-agent?) [closed]

如何检测用户是否正在通过移动Web浏览器查看我的网站,以便随后可以自动检测并显示适当版本的网站?


#1楼

参考:https://stackoom.com/question/4DU9/自动检测移动浏览器-通过用户代理


#2楼

Yes user-agent is used to detect mobile browsers. 是的,用户代理用于检测移动浏览器。 There are lots of free scripts available to check this. 有很多免费的脚本可用来检查这一点。 Here is one such php code which will help you redirect mobile users to different website. 这是一个这样的php代码 ,它将帮助您将移动用户重定向到其他网站。


#3楼

Yes, reading the User-Agent header will do the trick. 是的,读取User-Agent标头即可解决问题。

There are some lists out there of known mobile user agents so you don't need to start from scratch. 那里有一些已知的移动用户代理列表 因此您无需从头开始。 What I did when I had to is to build a database of known user agents and store unknowns as they are detected for revision and then manually figure out what they are. 我必须要做的是建立一个已知用户代理的数据库,并在检测到未知用户进行修订时存储未知信息,然后手动找出它们是什么。 This last thing might be overkill in some cases. 在某些情况下,这最后一件事可能会过大。

If you want to do it at Apache level, you can create a script which periodically generates a set of rewrite rules checking the user agent (or just once and forget about new user agents, or once a month, whatever suits your case), like 如果要在Apache级别执行此操作,则可以创建一个脚本,该脚本定期生成一组重写规则,以检查用户代理(或者一次并忘记新的用户代理,或者每月一次,视情况而定),例如

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} (OneMobileUserAgent|AnotherMobileUserAgent|...)
RewriteRule (.*) mobile/$1

which would move, for example, requests to http://domain/index.html to http://domain/mobile/index.html 例如,将请求从http://domain/index.html移到http://domain/mobile/index.html

If you don't like the approach of having a script recreate a htaccess file periodically, you can write a module which checks the User Agent (I didn't find one already made, but found this particularly appropriate example ) and get the user agents from some sites to update them. 如果您不喜欢让脚本定期重新创建htaccess文件的方法,则可以编写一个模块来检查用户代理(我没有找到一个,但找到了这个特别合适的示例 )并获取了用户代理。从某些网站进行更新。 Then you can complicate the approach as much as you want, but I think in your case the previous approach would be fine. 然后,您可以根据需要将方法复杂化,但是我认为您的情况下,以前的方法会很好。


#4楼

移动设备浏览器文件是检测ASP.NET项目的移动(和其他)浏览器的好方法: http ://mdbf.codeplex.com/


#5楼

You can detect mobile clients simply through navigator.userAgent , and load alternate scripts based on the detected client type as: 您可以仅通过navigator.userAgent检测移动客户端,并根据检测到的客户端类型加载备用脚本,如下所示:

 $(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
    }

    });

#6楼

检测移动浏览器”上有开源脚本,可以在Apache,ASP,ColdFusion,JavaScript和PHP中执行此操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值