JS Judge Mobile terminal (mobile browser)

Source: Internet
Author: User

Method 1:<script type="text/javascript">        varbrowser = {            versions: function () {                varu = 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.*/) , //是否为移动终端                    ios: !!u.match(/\(i[^;]+;( U;)? 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.mobile == true|| browser.versions.ios == true || browser.versions.android == true) {            if(confirm("检测到您是移动终端!是否跳转至手机版?"))            {                window.location.href = "http://3geas.huat.edu.cn";            }        }    </script>Two: <script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script><script type="text/javascript">uaredirect("http://www.163.com");</script>Here's how:
function Uaredirect (f)
{
try{
if (document.getElementById ("Bdmark")!=null)
{return}
var B=false;
if (Arguments[1]) {
var e=window.location.host;
var a=window.location.href;
if (Issubdomain (arguments[1],e) ==1) {
f=f+ "/#m/" +A;
B=true
}else{
if (Issubdomain (arguments[1],e) ==2) {
f=f+ "/#m/" +a;b=true
}else{
F=a;b=false}
}
}else{
B=true}
if (b) {
var C=window.location.hash;
if (!c.match ("Fromapp")) {
if (Navigator.userAgent.match (/(iphone|ipod| Android|ios)) {location.replace (f)}}}}catch (d) {}}function Issubdomain (c,d) {this.getdomain=function (f) {var e= F.indexof ("://"), if (e>0) {var h=f.substr (e+3)}else{var H=f}var g=/^www\./;if (g.test (h)) {H=h.substr (4)}return H}; if (C==d) {return 1}else{var c=this.getdomain (c); var b=this.getdomain (d); if (c==b) {return 1}else{c=c.replace (".", "\ \."); var a=new RegExp ("\ \.") +c+ "$"); if (B.match (a)) {return 2}else{return 0}}}
};
P.S.

"Web Development Compatibility series article (i): Different device browser useragent value Daquan" lists the common computer browser and smart phone browser useragent value

Here is the browser's respective useragent features to do an analysis, and give a judgment method:

Windows operating system Browser family:

  • IE Browser series:
    Features: All start with "mozilla/", "MSIE x.0;" x indicates its version;
    Judgment method: Rough judgment can only retrieve "MSIE x.0;" String can, strictly judge can retrieve "mozilla/x.0 (Compatibal; MSIE x.0; Windows NT ", but this is generally not necessary
  • Windows Edition Firefox:
    Feature: Start with "mozilla/x.0", including "Windows NT", "gecko/" and "firefox/";
    Judging method: Rough judge can only retrieve "firefox/" and "Windows NT" string, strictly judge can retrieve "mozilla/", "Windows NT", "gecko/" and "firefox/" four strings;
  • Chrome for Windows edition:
    Feature: Start with "mozilla/x.0", including "Windows NT", "chrome/", including "applewebkit/", "safari/";
    Judging method: Rough judge can only retrieve "Windows NT" and "chrome/" string, strictly judge can simultaneously retrieve "mozilla/", "Windows NT", "applewebkit/", "safari/", "chrome/" A string of five;
  • Windows Edition Opera:
    Feature: Start with "opera/", containing "Windows NT", "presto/" string;
    Judgment method: The Rough judgment only retrieves "Windows NT" and "opera/" string, strictly judge simultaneously retrieves "opera/", "Windows NT" and "presto/";
  • Windows version Safari:
    Feature: Start with "mozilla/", also contain "Windows NT", "applewebkit/", "safari/";
    Judging method: Rough judgment can be retrieved containing "Windows NT", "safari/" also does not include "chrome/", strict judgment needs to contain "mozilla/", "Windows NT", "applewebkit/", "safari/" But does not include "chrome/";
  • Summary: The browser useragent on the Windows operating system contains a "Windows NT" string to characterize the Windows operating system.


iphone Platform Browser series:
    • iphone comes with Safari:
      Features: With "mozilla/" beginning, containing "iphone" string, while containing "mobile/", "safari/" string;
      Judging method: Rough judge only to retrieve "iphone" and "safari/" string, strictly judge to include "mozilla/", "iphone", "mobile/", "safari/" four strings
    • The iphone version of Opera Mobile:
      Features: With "opera/" beginning, containing "iphone" string, while containing "opera mini/", "presto/" string;
      Judging method: Rough judgment only Retrieve "iphone" and "opera/" string, strictly judge to include "opera/", "iphone", "Opera mini/", "presto/" four strings
    • Summary: The browser useragent on the iphone contains the "iphone" string


Android Platform Browser series:
    • Android comes with a browser (some say it's chrome, but Google doesn't say it, and it's developing a chrome to Phone that runs on Android):
      Feature: Start with "mozilla/", containing "Android" and "Linux" string, while containing "applewebkit/", "mobile safari/" string;
      Judging method: Because there is no way to know whether Android will have a separate safari (estimated not), so it is recommended to strictly judge directly, search "mozilla/", "Android", "Linux", "applewebkit/", "mobile safari/"Five Strings
    • Android version of Opera Mobile:
      Features: Start with "opera/", containing "Android" and "Linux" string, while containing "opera mobi/", "presto/" string;
      Judging method: Rough judgment only to retrieve "Android" and "opera/", strictly judge to include "opera/", "Android", "Linux", "Opera mobi/", "presto/" five strings
    • Android version of Firefox:
      Features: With "mozilla/" beginning, containing "Android" and "Linux" string, while containing "firefox/", "gecko/", "fennec/" string;
      Judging method: Rough judgment only to retrieve "Android" and "firefox/", strictly judge to include "mozilla/", "Android", "Linux", "firefox/", "gecko/", "fennec/" six strings
    • Summary: The browser useragent on the Android platform contains both "Android" and "Linux" strings


The above three platforms for Windows, iphone, Android Mainstream browser resolution is basically over, the other platform Linux estimates at least similar to the Android platform, and the use of Mac OS ipad and Macintosh should be similar to the iphone platform, Therefore, for the time being not to do the analysis, but also because there are not so many devices and operating systems to test, I hope to be able to fill in the future.


Today's Web site product development requirements are not the same as before, because not only to meet the computer browsing, but also to meet the user through the smartphone (here refers to the iphone, Android, Windows Phone and other real smart phones, A small, semi-intelligent system like BlackBerry and palm is temporarily out of the question, as for Symbian, the pseudo-intelligent system, to play with it. Through the above three representative platforms, it is also possible to determine the solution of the user device based on the browser useragent.

1, if need to judge the operating system, the method is relatively simple, in the useragent to retrieve the following string:
    • Contains "Windows NT": It is obvious that the Windows operating system, NT version number can determine the OS version;
    • A system that contains "Mac": Apple's Mac OS X or other Mac OS kernel;
    • Contains "iphone": the Apple iphone is proprietary, in general should also contain "Mac";
    • Contains "ipad": Apple ipad tablet (data shows that the ipad browser useragent also contains "Mac", "iphone", "ipad");
    • Contains "Linux": Linux operating system or other Linux as the kernel operating system;
    • Contains "Android": Google's Android operating system, may be a smartphone, it may be the Android version of the Tablet, oh, in general, the Android platform useragent should also include "Linux";
    • Contains "Unix", "SunOS", "BSD" one of the three: Unix system, in fact, the user experience of this system problem, currently almost no need to consider;
    • Contains "Ubuntu": Ubuntu custom version of Linux
    • ......
As you can see, judging the operating system and its version is not always useful, but there is always a place to use it, such as developing a page that specifically targets screen resolutions for iphone, ipad, Android and other devices.


2, judge the browser's kernel, the method is not difficult, I figured out, not necessarily all right AH:
    • IE (Trident) kernel (ie for Mac, ies4linux and so on, not to mention, only consider under Windows): "mozilla/" beginning with "Windows NT" and "MSIE" string;
    • Firefox (Gecko) kernel: with "mozilla/" beginning, containing "firefox/" and "gecko/" string, which is the Android version of the "fennec/" string;
    • Opera () Kernel: Start with "opera/", containing "presto/" string, iphone version also with "Opera mini/", Android version also with "opera mobi/";
    • WebKit kernel: Start with "mozilla/", containing "applewebkit/" and "safari/" string, with "chrome/" is the Chrome browser, not with Safari or other;
    • That's the main browser kernel.
The browser kernel is the key to solve the compatibility problem, however, this compatibility issue has been the jquery and ExtJS framework to help you solve, so this decision only for the individual page CSS style in different kernel rendering effect of different situations, of course, It is also important to note that the same kernel renders different results on different devices, such as smartphones and computers.

3, to judge the actual application of browser Useagent example:
    • Different browser cores on the page rendering effect is different, although already have jquery and ExtJS and so on for us to do compatible processing, but there will still be some small differences need to be dealt with separately, we need to judge the browser kernel;
    • Users are not only through the computer to access the site, with the increasing popularity of smartphones and tablets of the big line, the use of both to the Internet is increasingly high proportion, how to do? Tablet PC OK, screen big resolution high, smartphone is limited by his screen size and resolution, although there is strong processing power, but also can perfectly support the existing site, but for customers to consider a little more than the harm you say, right? After all, through the local zoom drag-and-drop way to see the web is uncomfortable, then, we can specifically for the iphone, Android such a narrow screen to provide a special version to layout, one to enhance the browsing experience, and reduce network traffic, speed up access;
    • Analysis of visitor traffic, by judging the type of customer browser and recording its number, to optimize the design of their own site to enhance their customer experience, respectively
Laizi:http://www.cnblogs.com/dowinning/archive/2011/07/22/2113981.html

JS Judge Mobile terminal (mobile browser)

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.