如何使用jQuery或javascript检测您的设备是iPad3还是4
How to detect if your device is an iPad3 or 4 using jQuery or javascript?
我一直在寻找一些方法来检测iPad3和iPad4设备。我正在动态添加元视口。我想看到桌面版本并删除桌面,iPad3和iPad4中的视口,但在移动设备上当然添加视口以查看移动版本。
请在var ApplyViewPort中检查我的初始化函数:
var deviceDetection = function () {
var osVersion,
device,
deviceType,
userAgent,
isSmartphoneOrTablet;
device = (navigator.userAgent).match(/Android|iPhone|iPad|iPod/i);
if ( /Android/i.test(device) ) {
if ( !/mobile/i.test(navigator.userAgent) ) {
deviceType = 'tablet';
} else {
deviceType = 'phone';
}
osVersion = (navigator.userAgent).match(/Android's+(['d'.]+)/i);
osVersion = osVersion[0];
osVersion = osVersion.replace('Android ', '');
} else if ( /iPhone/i.test(device) ) {
deviceType = 'phone';
osVersion = (navigator.userAgent).match(/OS's+(['d'_]+)/i);
osVersion = osVersion[0];
osVersion = osVersion.replace(/_/g, '.');
osVersion = osVersion.replace('OS ', '');
} else if ( /iPad/i.test(device) ) {
deviceType = 'tablet';
osVersion = (navigator.userAgent).match(/OS's+(['d'_]+)/i);
osVersion = osVersion[0];
osVersion = osVersion.replace(/_/g, '.');
osVersion = osVersion.replace('OS ', '');
}
isSmartphoneOrTablet = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
userAgent = navigator.userAgent;
return { 'isSmartphoneOrTablet': isSmartphoneOrTablet,
'device': device,
'osVersion': osVersion,
'userAgent': userAgent,
'deviceType': deviceType
};
}();
var ApplyViewPort = {
init: function() {
this.metaView = '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />';
/// Preppend a meta viewport if it's not yet preppended
/// Else remove the meta viewport
if (deviceDetection.deviceType == 'tablet') {
$('meta[name="viewport"]').remove();
if (window.devicePixelRatio == 2) {
$('meta[name="viewport"]').remove();
}
} else {
$('head').append(this.metaView);
}
}
}
$(document).ready(function() {
ApplyViewPort.init();
});
http://docs.phonegap.com/en/3.0.0rc1/cordova_device_device.md.html#Device 试试这个有device.model;它将为您提供确切的iPad版本,但您只需要使用PhoneGap更高版本
在 iPad 1 和 2 步之间进行检测:
- 检查适用于 iPad 的 UA 字符串
- 检查陀螺仪
在 iPad 2 和 3 个步骤之间进行检测:
- 检查适用于 iPad 的 UA 字符串
- 檢查像素密度(Retina iPad 3 顯示器 = 2(
在 iPad 3 和 4 个步骤之间进行检测:
- 检查适用于 iPad 的 UA 字符串
- 检查像素密度(视网膜显示器 = 2(
- 检查设备最大各向异性(iPad 3 = 2,iPad 4 = 16(
最大各向异性为 16 通常表示具有良好图形性能的现代设备。
var gl;
var iPadVersion = false;
window.ondevicemotion = function(event) {
if (!iPadVersion && navigator.platform.indexOf("iPad") != -1) {
iPadVersion = 1;
if (event.acceleration) iPadVersion = window.devicePixelRatio;
}
window.ondevicemotion = null;
}
function initWebGL(canvas) {
gl = null;
try {
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
}
catch(e) {}
if (!gl) {
gl = null;
}
return gl;
}
function checkMaxAnisotropy() {
var max = 0;
var canvas = document.getElementById('webGLCanvasTest');
gl = initWebGL(canvas);
try {
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
}
catch(e) {}
if (gl) {
var ext = (
gl.getExtension('EXT_texture_filter_anisotropic') ||
gl.getExtension('MOZ_EXT_texture_filter_anisotropic') ||
gl.getExtension('WEBKIT_EXT_texture_filter_anisotropic')
);
if (ext){
max = gl.getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
}
}
return max;
}
function isiPad( $window ) {
var ua = $window.navigator.userAgent || $window.navigator.vendor || $window.opera;
return (/iPad/).test(ua);
}
function getiPadVersion( $window ) {
if(isiPad(window) && window.devicePixelRatio === 2) {
if(checkMaxAnisotropy() < 4) {
iPadVersion = 3;
} else {
iPadVersion = 4;
}
}
return iPadVersion;
}
/* BONUS CODE
isSmartDevice() - Detect most mobile devices
isOldDevice() - Detect older devices that have poor video card performance
*/
function isSmartDevice( $window ) {
var ua = $window.navigator.userAgent || $window.navigator.vendor || $window.opera;
return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
}
function isOldDevice() {
if(isSmartDevice(window) && window.devicePixelRatio === 1 && checkMaxAnisotropy() < 4 || isiPad( window ) && checkMaxAnisotropy() < 4) {
return true;
} else {
return false;
}
}
alert('iPad Version: ' + getiPadVersion(window) );
#webGLCanvasTest {
width: 1px;
height: 1px;
position: fixed;
top: -1px;
left: -1px;
}
<!-- Device Testing Canvas, Hide This Somewhere -->
<canvas id="webGLCanvasTest"></canvas>
相关文章:
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- 我应该使用Ng提交还是点击表格
- 在javascript中确定输入数字是负数还是正数
- 我应该先学习Angularjs还是Laravel
- 我需要NG-IF/NG-SWITCH还是NG-SHOW&NG-HIDE
- 最好使用JavaScript还是XQuery
- 仅当窗口宽度>940像素,无论是通过页面加载还是调整大小
- 内容脚本是在同一个孤立的世界中运行,还是在不同的孤立世界中运行
- 如何检测我的音频流是:正在播放、暂停还是已完成
- 使用appendChild()动态添加的脚本是按顺序执行还是并行执行
- 是重新呈现整个模板更好,还是使用jquery修改一些值更好
- 我应该总是将模块调用到var中,还是在节点中立即执行
- 是我的第三方JavaScript返回html还是只是一个空对象
- 区分客户端运行的是OSX还是Windows
- 最好检查tagName还是HTMLAnchorElement的instance
- 确定滑动事件是左滑动还是右滑动
- 将数据发布到iframe是同步还是异步
- CORS——它是客户端的东西,服务器端的东西,还是传输级别的东西
- 如何使用jQuery或javascript检测您的设备是iPad3还是4