JavaScript to determine terminal type examples

Source: Internet
Author: User

Just use JS to get the browser's useragent judgment.

JavaScript to judge the browser useragent is actually very simple, because JavaScript itself provides the appropriate action, in the Navigator object there is a useragent property, through the UserAgent property we can get, A string of useragent information

For example, we have to determine whether the browser is an ipad, so we can:

The code is as follows Copy Code
var isipad = Navigator.userAgent.match (/ipad/i)!= null;
if (Isipad) {
The corresponding code
}

Of course, you can do a finer thing, such as encapsulating the mainstream browser and making a judgment:

  code is as follows copy code

var Sys = {};
var ua = Navigator.userAgent.toLowerCase ();
if (window. ActiveXObject)
 sys.ie = Ua.match (/msie ([D.] +)/) [1]
Else if (document.getboxobjectfor)
 sys.firefox = Ua.match (/firefox/([D.] +)/) [1]
Else if (window. Messageevent &&!document.getboxobjectfor)
 sys.chrome = Ua.match (/chrome/([D.] +)/) [1]
Else if (window.opera)
 sys.opera = Ua.match (/opera. ( [D.] +)/) [1]
Else if (window.opendatabase)
 sys.safari = Ua.match (/version/([D.] +)/) [1];

The pursuit of perfect endless, can also do better and meticulous, such as Judge version, Judge is not a mobile browser, but if you consider the mobile phone, whether to support JavaScript and other factors to consider, the appropriate time to consider other procedures to solve

Example

The code is as follows Copy Code

var browser={
Versions:function () {
var u = navigator.useragent, app = Navigator.appversion;
return {
Trident:u.indexof (' Trident ') >-1,//ie kernel
Presto:u.indexof (' presto ') >-1,//opera kernel
Webkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernel
Gecko:u.indexof (' Gecko ') >-1 && u.indexof (' khtml ') = = 1,//Firefox kernel
Mobile:!! U.match (/applewebkit.*mobile/) | | !! U.match (/windows phone/) | | !! U.match (/android/) | | !! U.match (/mqqbrowser/),
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 the ipad
Webapp:u.indexof (' Safari ') = = 1/Whether the Web should program, without head and bottom
};
}()
}

Document.writeln ("Whether for mobile terminal www.111cn.net:" +browser.versions.mobile);
Document.writeln ("iOS terminal:" +browser.versions.ios);
Document.writeln ("Android Terminal:" +browser.versions.android);
Document.writeln ("Whether for IPhone:" +browser.versions.iphone);
Document.writeln ("Whether the IPad:" +browser.versions.ipad);
Document.writeln (navigator.useragent);

More special place.

UC Browser does not have an Android header, only return: Linux, here roughly based on Linux to judge is Android (the premise must be satisfied with the mobile terminal, UC this is satisfied)

Android QQ Browser HD version of the results are: Mac, Safari, this is very sick, I look at the deal with it

Add an example

For example, to determine whether a cell phone or a computer

  code is as follows copy code

function Browserredirect () {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 | | bisiphoneos | | bismidp | | bIsUc7 | | bisuc | | bisandroid | | bisce | | biswm) {alert ("I am Mobile");} else { Alert ("I am PC"); } }

Cloud Habitat Community Small remind you: PHP version we use PHP $_server[' http_user_agent '] to get mobile user browser user agent, and then match the existing various mobile browser agent library, if it contains matching keywords, The Mobile (mobile terminal) user is judged to be

Related Article

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.