isMobile 一个简单的JS库,用来检测移动设备
github地址: https://github.com/kaimallea/isMobile
示例
(function () { var MOBILE_SITE = 'http://m.xx.com/index.html', NO_REDIRECT = 'noredirect'; if (isMobile.any) { if ( document.cookie.indexOf(NO_REDIRECT) === -1 ) { document.location = MOBILE_SITE; } } })();
Why use isMobile?
I had a specific requirement for a project when I created this:
- Redirect all iPhones, iPods, Android phones, and seven inch devices to the mobile site.
A completely separate site had been created for mobile devices, so feature detection/graceful degredation/progressive enhancement were out of the question. I had to redirect.
I couldn’t do detection on the back-end, because the entire site was cached and served by Akamai; I had to do the detection client-side.
So I resorted to UA sniffing.
I tried to keep the script small ( currently ~1.3k bytes, minified ) and simple, because it would need to execute in the <head>
, which is generally a bad idea, since JS blocks downloading and rendering of anything else while it parses and executes. In the case of mobile redirection, I don’t mind so much, because I want to start the redirect as soon as possible, before the device has a chance to start downloading and rendering stuff. For non-mobile platforms, the script should execute fast, so the browser can quickly get back to downloading and rendering.
How it works
isMobile runs quickly on page load to detect mobile devices; it then creates a JavaScript object with the results.
Devices detected by isMobile
The following properies of the isMobile
object will either be true
or false
Apple devices
isMobile.apple.phone
isMobile.apple.ipod
isMobile.apple.tablet
isMobile.apple.device
(any mobile Apple device)
Android devices
isMobile.android.phone
isMobile.android.tablet
isMobile.android.device
(any mobile Android device)
Windows devices
isMobile.windows.phone
isMobile.windows.tablet
isMobile.windows.device
(any mobile Windows device)
Specific seven inch devices
isMobile.seven_inch
true
if the device is one of the following 7″ devices:- Nexus 7
- Kindle Fire
- Nook Tablet 7 inch
- Galaxy Tab 7 inch
“Other” devices
isMobile.other_blackberry_10
isMobile.other_blackberry
isMobile.other_opera
(Opera Mini)isMobile.other_firefox
Aggregate Groupings
isMobile.any
- any device matchedisMobile.phone
- any device in the ‘phone’ groups aboveisMobile.tablet
- any device in the ‘tablet’ groups above
isMobile 一个简单的JS库,用来检测移动设备相关推荐
- 鸿蒙系统测试失败,ANR-WatchDog-ohos: 一个简单的监测程序,可检测到鸿蒙系统的 ANR(Application Not Response-应用程序无响应)错误并引发有意义的异常...
ANR-WatchDog-ohos 一个简单的监测程序,可检测到鸿蒙系统的 ANR(Application Not Response-应用程序无响应)错误并引发有意义的异常 项目名称:ANR-Watc ...
- 如何使用装饰器创建一个简单的UI库
目录 概念 设置项目 创建装饰器 基类 完成笑脸元素 下一步是什么? 下载最新源(GitHub) 下载Tiny概念 概念 下面的代码揭示了我脑海中的概念.它表示一个简单的Web组件,可根据输入显示不同 ...
- 设计一个简单的[JS]文本摘要算法
引 网站cookie中有一段用户信息(包括微信用户信息,用户关联信息)的文本,使用entrance键存储,选择不同的条件后文本内容也不同,cookie上的值也会变化.本来的打算是通过将entrance ...
- date-fns日期格式化_date-fns快速浏览,一个简单JavaScript日期库
date-fns日期格式化 Working with dates in JavaScript isn't easy. That's why if you look at the package.jso ...
- 一个简单的js闹钟程序
一个基于js的简单闹钟程序,小白专用. <script type="text/javascript">var date = new Date();var hour = ...
- 一个简单的 js 时间对象创建
JS中获取时间很常见,凑凑热闹,也获取一个时间对象试试 首先,先了解js的获取时间函数如下: var myDate = new Date(); //创建一个时间对象 myDate.g ...
- 一个简单的JS(盒子移动)
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...
- 一个简单的JS异步改为同步例子
先看一段代码 setTimeout(function() {console.log('123') }, 500) console.log('456') 对于初学者来讲,会想着代码是从上往下一行一行执行 ...
- html 表格自动计算,一个简单的js事件,循环table并自动计算总价
texter function $get(id){ return document.getElementById(id); } var table=null; function caculPrice( ...
最新文章
- Spring集成线程池
- Android Studio 配置虚拟设备的镜像文件的存放路径
- 计算机科学与技术专业用英语怎么写,计算机科学与技术专业专业英文简历模板...
- Cloud for Customer custom BO创建时间随着行项目数量增加而增加的关系
- 计算机基础知识的重点,计算机基础知识重点
- 邻近算法(KNN算法)
- 非关型数据库之MongoDB
- 推荐几个 Go 测试工具和技巧让你在虎年如虎添翼
- matlab中count是干什么,matlab编程潮流计算的count值与什么有?
- mysql投票网站_PHP+Mysql实现网站顶和踩投票功能实例
- python __iter____next__
- 搭建离线版keras中文文档
- CSS+html制作简历表
- H.264 NAL语法语意以及字节流的语法语意
- python写文字冒险游戏手机版_用128行代码实现一个文字冒险游戏
- TCP/IP 模型 与 OSI 七层模型的对应关系
- 【详解】Executors框架之Executors
- Cognos入门教程
- Leaflet加载百度地图
- Codeforces Bear and Big Brother