javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5632字,纯文字阅读大概需要9分钟。
内容图文
![javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等](/upload/InfoBanner/zyjiaocheng/1126/ecbb81dc36934cf78ed9af80741cb1f7.jpg)
使用device.js检测设备并实现不同设备展示不同网页
html代码:
<! doctype html > < html > < head > < meta charset ="utf-8" > < title ></ title > < script src ="device.js" ></ script > </ head > < body style ="margin: auto; position: absolute; width:100%; height: 100%" > < script > var isMobile = device.mobile(), isIos = device.ios(), isAndroid = device.android(); if (isMobile){ alert( " 手机 " ); } else { alert( " pc " ); } if (isIos){ alert( " ios " ); } if (isAndroid){ alert( ‘ 安卓 ‘ ); } </ script > </ body > </ html >
引入device.js
(function() { var previousDevice, _addClass, _doc_element, _find, _handleOrientation, _hasClass, _orientation_event, _removeClass, _supports_orientation, _user_agent; previousDevice = window.device; window.device = {}; _doc_element = window.document.documentElement; _user_agent = window.navigator.userAgent.toLowerCase(); device.ios = function() { return device.iphone() || device.ipod() || device.ipad(); }; device.iphone = function() { return _find(‘iphone‘); }; device.ipod = function() { return _find(‘ipod‘); }; device.ipad = function() { return _find(‘ipad‘); }; device.android = function() { return _find(‘android‘); }; device.androidPhone = function() { return device.android() && _find(‘mobile‘); }; device.androidTablet = function() { return device.android() && !_find(‘mobile‘); }; device.blackberry = function() { return _find(‘blackberry‘) || _find(‘bb10‘) || _find(‘rim‘); }; device.blackberryPhone = function() { return device.blackberry() && !_find(‘tablet‘); }; device.blackberryTablet = function() { return device.blackberry() && _find(‘tablet‘); }; device.windows = function() { return _find(‘windows‘); }; device.windowsPhone = function() { return device.windows() && _find(‘phone‘); }; device.windowsTablet = function() { return device.windows() && _find(‘touch‘); }; device.fxos = function() { return (_find(‘(mobile;‘) || _find(‘(tablet;‘)) && _find(‘; rv:‘); }; device.fxosPhone = function() { return device.fxos() && _find(‘mobile‘); }; device.fxosTablet = function() { return device.fxos() && _find(‘tablet‘); }; device.meego = function() { return _find(‘meego‘); }; device.mobile = function() { return device.androidPhone() || device.iphone() || device.ipod() || device.windowsPhone() || device.blackberryPhone() || device.fxosPhone() || device.meego(); }; device.tablet = function() { return device.ipad() || device.androidTablet() || device.blackberryTablet() || device.windowsTablet() || device.fxosTablet(); }; device.portrait = function() { return Math.abs(window.orientation) !== 90; }; device.landscape = function() { return Math.abs(window.orientation) === 90; }; device.noConflict = function() { window.device = previousDevice; returnthis; }; _find = function(needle) { return _user_agent.indexOf(needle) !== -1; }; _hasClass = function(class_name) { var regex; regex = new RegExp(class_name, ‘i‘); return _doc_element.className.match(regex); }; _addClass = function(class_name) { if (!_hasClass(class_name)) { return _doc_element.className += " " + class_name; } }; _removeClass = function(class_name) { if (_hasClass(class_name)) { return _doc_element.className = _doc_element.className.replace(class_name, ""); } }; if (device.ios()) { if (device.ipad()) { _addClass("ios ipad tablet"); } elseif (device.iphone()) { _addClass("ios iphone mobile"); } elseif (device.ipod()) { _addClass("ios ipod mobile"); } } elseif (device.android()) { if (device.androidTablet()) { _addClass("android tablet"); } else { _addClass("android mobile"); } } elseif (device.blackberry()) { if (device.blackberryTablet()) { _addClass("blackberry tablet"); } else { _addClass("blackberry mobile"); } } elseif (device.windows()) { if (device.windowsTablet()) { _addClass("windows tablet"); } elseif (device.windowsPhone()) { _addClass("windows mobile"); } else { _addClass("desktop"); } } elseif (device.fxos()) { if (device.fxosTablet()) { _addClass("fxos tablet"); } else { _addClass("fxos mobile"); } } elseif (device.meego()) { _addClass("meego mobile"); } else { _addClass("desktop"); } _handleOrientation = function() { if (device.landscape()) { _removeClass("portrait"); return _addClass("landscape"); } else { _removeClass("landscape"); return _addClass("portrait"); } }; _supports_orientation = "onorientationchange" in window; _orientation_event = _supports_orientation ? "orientationchange" : "resize"; if (window.addEventListener) { window.addEventListener(_orientation_event, _handleOrientation, false); } elseif (window.attachEvent) { window.attachEvent(_orientation_event, _handleOrientation); } else { window[_orientation_event] = _handleOrientation; } _handleOrientation(); }).call(this);
api列表:
|
实例下载:设备检测demo(http://files.cnblogs.com/zhidong123/devide-test.rar)
原文:http://www.cnblogs.com/zhidong123/p/3806672.html
内容总结
以上是互联网集市为您收集整理的javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等全部内容,希望文章能够帮你解决javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。