博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
三种识别目标为移动设备的方法
阅读量:6234 次
发布时间:2019-06-21

本文共 4777 字,大约阅读时间需要 15 分钟。

We recently launched a beta version of a New Media Campaigns mobile Webkit site.  If you're reading this post from an iPhone, iTouch, Palm Pre or Android based phone you're seeing it right now.  Josh Lockhart did the design and frontend code for the site, a topic he will be covering in a post later this week.

This post discusses how to target mobile devices and show them either different content, different stylesheets or even redirect to a mobile URL. Ideally, the check would be done for ALL mobile webkit platforms, not just the iPhone.  It would be a shame to leave those Android/Palm out after developing a pretty mobile site. So without further ado, here are the methods:

1. Checking User Agent Serverside

When a browser visits a site, it sends a string describing who it is called the user-agent string.  It varies depending on the browser and platform.  The user-agent string for key mobile webkit browsers are:

iPhone - Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3iTouch - Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3Android - Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3Palm Pre - Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0

 

Unfortunately there is not a good way to check for mobile webkit in general.  So each of these needs to be checked for in a case by case manner.  Here is some rough php to do that:

if( strstr($_SERVER['HTTP_USER_AGENT'],'Android') ||    strstr($_SERVER['HTTP_USER_AGENT'],'webOS') ||    strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') ||    strstr($_SERVER['HTTP_USER_AGENT'],'iPod')    ){    // Send Mobile Site}

We did this type detection since we wanted to send completely new templates for pages at the same URL.  This worked nicely with our CMS since both the normal frontend and the mobile fronend were updated from the same database.  We could also use this technique to redirect to a mobile URL if that is how we wanted to present our mobile site.

2. Checking User Agent Clientside

This method simply uses javascript to check the User Agent after the page has loaded.  The obvious downside is that it first requires mobile visitors to load your standard site which might be fairly heavy.  It is fine to use in a pinch where you are not able to modify server-side code.

Here is some javascript to detect the mobile webkit browsers:

if( navigator.userAgent.match(/Android/i) ||    navigator.userAgent.match(/webOS/i) ||    navigator.userAgent.match(/iPhone/i) ||    navigator.userAgent.match(/iPod/i) ||    ){ // Send Mobile Site}

 

PS:

var sUserAgent = navigator.userAgent.toLowerCase();        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";        var bIsMidp = sUserAgent.match(/midp/i) == "midp";        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";        var bIsAndroid = sUserAgent.match(/android/i) == "android";        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";                if (bIsIpad || bIsIphoneOs || bIsAndroid || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM) {           return false;        }else{           return true;        }

 

You can use this method to either redirect to a different site, send a different stylesheet or whatever else you may need to do for a mobile site.

3. Use CSS Media Type

If your HTML doesn't need to change between your mobile site and standard site, it may make more sense to send a different stylesheet just to mobile browsers.  There are a couple of ways to do this, but using the media type capabilities of CSS may be the way to go.

To load an entirely different stylesheet, use the media attribute when loading the stylesheet:

Alternatively you can just add some declarations into your existing stylesheet:

@media screen and (max-device-width: 480px) {    /* mobile declarations */}

This method is a little dangerous since it depends on the screen resolution to send the stylesheet. A mobile site is generally not different from a typical site because of resolution alone; it is also different because of the physical screen size of the device.  For instance, the new Verizon Droid has roughly the same screen dimensions of the iPhone but a resolution of 854px by 440px!  Serving a mobile site to this device makes sense even though it has such high resolution.

If you've done a mobile site and have a perferred method be sure to share the site and the method in the comments.

 

转:

转载地址:http://ermna.baihongyu.com/

你可能感兴趣的文章
量子通信和大数据最有市场突破前景
查看>>
对‘初学者应该选择哪种编程语言’的回答——计算机达人成长之路(38)
查看>>
如何申请开通微信多客服功能
查看>>
Sr_C++_Engineer_(LBS_Engine@Global Map Dept.)
查看>>
非监督学习算法:异常检测
查看>>
jquery的checkbox,radio,select等方法总结
查看>>
Linux coredump
查看>>
Ubuntu 10.04安装水晶(Mercury)无线网卡驱动
查看>>
我的友情链接
查看>>
nginx在reload时候报错invalid PID number
查看>>
ElasticSearch 2 (32) - 信息聚合系列之范围限定
查看>>
VS2010远程调试C#程序
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>
Python3.4 12306 2015年3月验证码识别
查看>>
从Handler.post(Runnable r)再一次梳理Android的消息机制(以及handler的内存泄露)
查看>>
windows查看端口占用
查看>>
Yii用ajax实现无刷新检索更新CListView数据
查看>>
JDBC的事务
查看>>
Io流的概述
查看>>
App 卸载记录
查看>>