Home Web Front-end JS Tutorial js determines the mobile phone (Android phone or iPhone phone)_javascript skills

js determines the mobile phone (Android phone or iPhone phone)_javascript skills

May 16, 2016 pm 03:49 PM
android iphone js Mobile terminal

Commonly used codes on the Internet

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

/**

 * [isMobile 判断平台]

 * @param test: 0:iPhone    1:Android

 */

function ismobile(test){

    var u = navigator.userAgent, app = navigator.appVersion;

    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){

     if(window.location.href.indexOf("&#63;mobile")<0){

      try{

       if(/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)){

        return '0';

       }else{

        return '1';

       }

      }catch(e){}

     }

    }else if( u.indexOf('iPad') > -1){

        return '0';

    }else{

        return '1';

    }

};

Copy after login

How to use:

var pla=ismobile(1);

If pla returns 0:iPhone 1:Android

Code 1,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<script type="text/javascript">

var browser = {

  versions: function () {

  var u = navigator.userAgent, app = navigator.appVersion;

  return {//移动终端浏览器版本信息

   trident: u.indexOf('Trident') > -1, //IE内核

   presto: u.indexOf('Presto') > -1, //opera内核

   webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核

   gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核

   mobile: !!u.match(/AppleWebKit.*Mobile/i) || !!u.match(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/), //是否为移动终端

   ios: !!u.match(/\(i[^;]+;( U;)&#63; CPU.+Mac OS X/), //ios终端

   android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器

   iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器

   iPad: u.indexOf('iPad') > -1, //是否iPad

   webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部

  };

  } (),

  language: (navigator.browserLanguage || navigator.language).toLowerCase()

}

if (browser.versions.iPhone || browser.versions.iPad || browser.versions.ios) {

window.location.href = "http://www.jb51.net";

}

if (browser.versions.android) {

window.location.href = "http://www.qq.com";

}

</script>

Copy after login

How to determine whether it is an iPad browser? The key is to see if there is an iPad in its User Agent. The iPad uses the Safari Mobile browser, and its User Agent is:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

Javascript code
function is_iPad(){
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/iPad/i)=="ipad") {
return true;
} else {
return false;
}
}
Therefore, the code to determine ipad, iphone, android is:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

<script type="text/javascript">

var bForcepc = fGetQuery("dv") == "pc";

function fBrowserRedirect(){

 var sUserAgent = navigator.userAgent.toLowerCase();

 var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";

 var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";

 var bIsMidp = sUserAgent.match(/midp/i) == "midp";

 var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";

 var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";

 var bIsAndroid = sUserAgent.match(/android/i) == "android";

 var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";

 var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

 if(bIsIpad){

 var sUrl = location.href;

 if(!bForcepc){

  window.location.href = "http://m.jb51.net/&#63;ipad";

 }

 }

 if(bIsIphoneOs || bIsAndroid){

 var sUrl = location.href;

 if(!bForcepc){

  window.location.href = "http://m.jb51.net/&#63;iphone";

 }

 }

 if(bIsMidp||bIsUc7||bIsUc||bIsCE||bIsWM){

 var sUrl = location.href;

 if(!bForcepc){

  window.location.href = "http://m.jb51.net/";

 }

 }

}

function fGetQuery(name){//获取参数值

 var sUrl = window.location.search.substr(1);

 var r = sUrl.match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"));

 return (r == null &#63; null : unescape(r[2]));

}

function fShowVerBlock(){

 if(bForcepc){

 document.getElementById("dv_block").style.display = "block";

 }

 else{

 document.getElementById("ad_block").style.display = "block";

 }

}

fBrowserRedirect();

</script>

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Oppo Find X8 design looks like a cross between Apple iPhone 16 Pro and OnePlus Open in early images Oppo Find X8 design looks like a cross between Apple iPhone 16 Pro and OnePlus Open in early images Sep 28, 2024 am 06:04 AM

Historically, Oppo has refreshed its flagship 'Find X' series in late winter or early spring, save for the original Find X that it announced in June 2018. To that end, the Find X7 and Find X7 Ultra are barely more than six months old at this point. H

Gate.io trading platform official app download and installation address Gate.io trading platform official app download and installation address Feb 13, 2025 pm 07:33 PM

This article details the steps to register and download the latest app on the official website of Gate.io. First, the registration process is introduced, including filling in the registration information, verifying the email/mobile phone number, and completing the registration. Secondly, it explains how to download the Gate.io App on iOS devices and Android devices. Finally, security tips are emphasized, such as verifying the authenticity of the official website, enabling two-step verification, and being alert to phishing risks to ensure the safety of user accounts and assets.

How to solve the problem of 'Undefined array key 'sign'' error when calling Alipay EasySDK using PHP? How to solve the problem of 'Undefined array key 'sign'' error when calling Alipay EasySDK using PHP? Mar 31, 2025 pm 11:51 PM

Problem Description When calling Alipay EasySDK using PHP, after filling in the parameters according to the official code, an error message was reported during operation: "Undefined...

Lenovo reveals new color option for the 2024 Legion Y700 gaming tablet Lenovo reveals new color option for the 2024 Legion Y700 gaming tablet Sep 29, 2024 am 06:05 AM

Lenovo is gearing up to launch the 2024 Legion Y700 on September 29 in China. This new Android gaming tablet will be going against the RedMagic Nova, and the company has already confirmed almost all the specs of the device. Now, hours before the full

Anbi app official download v2.96.2 latest version installation Anbi official Android version Anbi app official download v2.96.2 latest version installation Anbi official Android version Mar 04, 2025 pm 01:06 PM

Binance App official installation steps: Android needs to visit the official website to find the download link, choose the Android version to download and install; iOS search for "Binance" on the App Store. All should pay attention to the agreement through official channels.

Multi-party certification: iPhone 17 standard version will support high refresh rate! For the first time in history! Multi-party certification: iPhone 17 standard version will support high refresh rate! For the first time in history! Apr 13, 2025 pm 11:15 PM

Apple's iPhone 17 may usher in a major upgrade to cope with the impact of strong competitors such as Huawei and Xiaomi in China. According to the digital blogger @Digital Chat Station, the standard version of iPhone 17 is expected to be equipped with a high refresh rate screen for the first time, significantly improving the user experience. This move marks the fact that Apple has finally delegated high refresh rate technology to the standard version after five years. At present, the iPhone 16 is the only flagship phone with a 60Hz screen in the 6,000 yuan price range, and it seems a bit behind. Although the standard version of the iPhone 17 will have a high refresh rate screen, there are still differences compared to the Pro version, such as the bezel design still does not achieve the ultra-narrow bezel effect of the Pro version. What is more worth noting is that the iPhone 17 Pro series will adopt a brand new and more

How to view XML on your phone without a network How to view XML on your phone without a network Apr 02, 2025 pm 10:30 PM

There are two ways to view XML files: Android phones: use file manager or third-party applications (XML Viewer, DroidEdit). iPhone: Transfer files via iCloud Drive and use the Files app or third-party app (XML Buddha, Textastic).

See all articles