JS to determine the phone side (Android phone or iphone)

Source: Internet
Author: User
Tags lenovo

More and more people are now using their mobile phones to surf the web, and some download sites can access different Web pages, such as iphones and Android, by judging different system phones. Let's look at how to use JavaScript to judge iphone or Android phone access

Code commonly used on the internet

/**
* [IsMobile judgment Platform]
* @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 ("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 ';
}
};

How to use:

var pla=ismobile (1);

If the PLA returns a 0:iphone 1:android

Code One,
<script type= "Text/javascript" >
var browser = {
Versions:function () {
var u = navigator.useragent, app = Navigator.appversion;
return {//Mobile terminal browser version information
Trident:u.indexof (' Trident ') >-1,//ie core
Presto:u.indexof (' presto ') >-1,//opera core
Webkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernel
Gecko:u.indexof (' Gecko ') >-1 && u.indexof (' khtml ') = =-1,//Firefox kernel
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/),//Whether it is a mobile terminal
Ios:!! U.match (/\ (i[^;] +;( U;)? Cpu.+mac OS x/),//ios terminal
Android:u.indexof (' Android ') >-1 | | U.indexof (' Linux ') >-1,//android terminal or UC Browser
Iphone:u.indexof (' IPhone ') >-1 | | U.indexof (' Mac ') >-1,//whether for iphone or Qqhd browser
Ipad:u.indexof (' ipad ') >-1,//whether ipad
Webapp:u.indexof (' Safari ') = =-1//whether the Web should be program, no head with bottom
};
} (),
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>

How to tell if it's an ipad browser, the key is to see if it has an ipad in the User Agent. The IPad uses the Safari Mobile browser, and his 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 Judge Ipad,iphone,android is:
<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/?ipad";
}
}
if (Bisiphoneos | | bisandroid) {
var surl = location.href;
if (!BFORCEPC) {
Window.location.href = "Http://m.jb51.net/?iphone";
}
}
if (bismidp| | bisuc7| | bisuc| | bisce| | BISWM) {
var surl = location.href;
if (!BFORCEPC) {
Window.location.href = "http://m.jb51.net/";
}
}
}
function Fgetquery (name) {//Get parameter value
var surl = window.location.search.substr (1);
var r = Surl.match (New RegExp ("(^|&)" + name + "= ([^&]*) (&|$)");
return (r = = null? 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>

JS to determine the phone side (Android phone or iphone)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.