如何使用jquery检测浏览器是iOS,android还是桌面?

时间:2015-09-14 17:00:31

标签: android jquery ios desktop browser-detection

这个想法是制作一个按钮,说明现在就可以获得应用程序"根据是否是Android手机iPhone或没有更改链接。我查看了stackexchange的答案,但对于没有js经验的人来说,没有什么是明确的

3 个答案:

答案 0 :(得分:1)

//检查设备是移动设备还是桌面设备。

if(jQuery.browser.mobile)
{
   console.log('You are using a mobile device!');
}
else
{
   console.log('You are not using a mobile device!');
}

//检测iPad

var isiPad = /ipad/i.test(navigator.userAgent.toLowerCase());
if (isiPad)
{
  ...
}

//检测iPhone

var isiPhone = /iphone/i.test(navigator.userAgent.toLowerCase());
if (isiPhone)
{
  ...
}

//检测iPod

var isiPod = /ipod/i.test(navigator.userAgent.toLowerCase());
if (isiPod)
{
  ...
}

//检测iDevice

var isiDevice = /ipad|iphone|ipod/i.test(navigator.userAgent.toLowerCase());

if (isiDevice)
{
  ...
}

//检测Andriod

var isAndroid = /android/i.test(navigator.userAgent.toLowerCase());

if (isAndroid)
{
  ...
}

//检测黑莓

var isBlackBerry = /blackberry/i.test(navigator.userAgent.toLowerCase());

if (isBlackBerry)
{
  ...
}

//检测WebOs

var isWebOS = /webos/i.test(navigator.userAgent.toLowerCase());

if (isWebOS)
{
  ...
}

//检测Windows Phone

var isWindowsPhone = /windows phone/i.test(navigator.userAgent.toLowerCase());
if (isWindowsPhone)
{
  ...
}

上面提到的代码就是我在以下链接中找到的代码。 https://www.sitepoint.com/detect-mobile-devices-jquery/

答案 1 :(得分:0)

试试这个,

未知是桌面电话或任何其他电话

function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

  if( userAgent.match( /iPad/i ) || userAgent.match( /iPhone/i ) || userAgent.match( /iPod/i ) )
  {
    return 'iOS';

  }
  else if( userAgent.match( /Android/i ) )
  {

    return 'Android';
  }
  else
  {
    return 'unknown'; 
  }
}

来源:Detecting iOS / Android Operating system

答案 2 :(得分:0)

js中的Tom Landau的答案<= ES5(使用“旧”语言语法):

/**
 * @summary
 * utils.isMobile
 * --------------
 * @desc
 * Creates a global object with member functions that test if the client's browser 
 * belongs to a specific family of devices.
 * @function utils.isMobile
 * @return {Boolean}
 * @see {@link https://stackoverflow.com/questions/32570067/how-to-detect-whether-browser-is-ios-android-or-desktop-using-jquery}, 
 * {@link https://bitsrc.io/tomlandau/simple-js/global/is-mobile/code#src/global/isMobile.js}
 */
utils = window.utils || {};
utils.isMobile = {
   getUserAgent: function() {
      return navigator.userAgent;
   },
   Android: function() {
      return /Android/i.test(this.getUserAgent()) && !this.Windows();
   },
   BlackBerry: function() {
      return /BlackBerry|BB10|PlayBook/i.test(this.getUserAgent());;
   },
   iPhone: function() {
      return /iPhone/i.test(this.getUserAgent()) && !this.iPad() && !this.Windows();
   },
   iPod: function() {
      return /iPod/i.test(this.getUserAgent());
   },
   iPad: function() {
      return /iPad/i.test(this.getUserAgent());
   },
   iOS: function() {
      return (this.iPad() || this.iPod() || this.iPhone());
   },
   Opera: function() {
      return /Opera Mini/i.test(this.getUserAgent());
   },
   Windows: function() {
      return /Windows Phone|IEMobile|WPDesktop/i.test(this.getUserAgent());
   },
   KindleFire: function() {
      return /Kindle Fire|Silk|KFAPWA|KFSOWI|KFJWA|KFJWI|KFAPWI|KFAPWI|KFOT|KFTT|KFTHWI|KFTHWA|KFASWI|KFTBWI|KFMEWI|KFFOWI|KFSAWA|KFSAWI|KFARWI/i.test(this.getUserAgent());
   },
   any: function() {
      return (this.Android() || this.BlackBerry() || this.iOS() || this.Opera() || this.Windows());
   }
};

像往常一样经过测试:if(utils.isMobile.any())...if(utils.isMobile.Android())...