Chinaunix首页 | 论坛 | 博客
  • 博客访问: 192251
  • 博文数量: 43
  • 博客积分: 366
  • 博客等级: 一等列兵
  • 技术积分: 427
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-17 14:03
文章分类

全部博文(43)

文章存档

2018年(2)

2017年(5)

2016年(2)

2015年(3)

2014年(9)

2013年(5)

2012年(8)

2011年(9)

我的朋友

分类: LINUX

2014-07-29 11:39:33

同一套判断方式的两种写法

php:

点击(此处)折叠或打开

  1. class BrowserDetector {
  2.     public $UA = ""; //$HTTP_USER_AGENT的内容
  3.     public $BROWSER_VERSION = array();
  4.     
  5.     /* 构造函数开始 */
  6.     function __construct(){
  7.         $this->UA = getenv(HTTP_USER_AGENT);
  8.         $this->BROWSER_VERSION = array(
  9.                 "trident"=> false !== stripos($this->UA, 'Trident'), //IE内核
  10.                 "presto"=> false !== stripos($this->UA, 'Presto'), //opera内核
  11.                 "webKit"=> false !== stripos($this->UA, 'AppleWebKit'), //苹果、谷歌内核
  12.                 "gecko"=> false !== stripos($this->UA, 'Gecko') && false === stripos($this->UA, 'KHTML'), //火狐内核
  13.                 "mobile"=> 0 < preg_match('/AppleWebKit.*Mobile.*/i', $this->UA)
  14.                             || 0 < preg_match('/AppleWebKit/i', $this->UA), //是否为移动终端
  15.                 "ios"=> 0 < preg_match('/(i[^;]+\;(U;)? CPU.+Mac OS X)/i', $this->UA), //ios终端
  16.                 "android"=> false !== stripos($this->UA, 'Android') || false !== stripos($this->UA, 'Linux'), //android终端或者uc浏览器
  17.                 "iPhone"=> false !== stripos($this->UA, 'iPhone') || false !== stripos($this->UA, 'Mac'), //是否为iPhone或者QQHD浏览器
  18.                 "iPad"=> false !== stripos($this->UA, 'iPad'), //是否iPad
  19.                 "microMessenger"=> 0 < preg_match('/MicroMessenger/i', $this->UA),
  20.                 "webApp"=> false === stripos($this->UA, 'Safari') //是否web应该程序,没有头部与底部
  21.         );
  22.     }
  23. }


js版本:

点击(此处)折叠或打开

  1. var browser={

  2.         versions:function(){
  3.             var u = navigator.userAgent, app = navigator.appVersion;

  4.             return {
  5.                 trident: u.indexOf('Trident') > -1, //IE内核
  6.                 presto: u.indexOf('Presto') > -1, //opera内核
  7.                 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  8.                 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  9.                 mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否为移动终端
  10.                 ios: !!u.match(/(i[^;]+\;(U;)? CPU.+Mac OS X)/), //ios终端
  11.                 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
  12.                 iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
  13.                 iPad: u.indexOf('iPad') > -1, //是否iPad
  14.                 microMessenger: (null != u.match(/MicroMessenger/i)),
  15.                 webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
  16.             };
  17.         }(),
  18.         language:(navigator.browserLanguage || navigator.language).toLowerCase()
  19.     }


阅读(1122) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~