Use JavaScript to determine if a user is a mobile device

Source: Internet
Author: User

Recently in do service number development, which encountered a problem is the service number to view the personal messages, if clicked in the browser view (in iOS is open in Safari) should be to jump to the login page, because the page needs to be retrieved from the background, because the server needs to be judged, If the storage page does not need background data can be judged in the foreground, because the background is nodejs, so give the client and server two version of the Code for reference.

Client Judgment

The method is very simple, is through useragent to judge, first judge whether is mobile end, can judge is the iOS terminal and the Android terminal, can also be specific to the application to judge, microblogging, QQ visit:

var browser = {
Versions:function () {
var u = navigator.useragent,
UA = Navigator.userAgent.toLowerCase ();
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.*/),//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,//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
Wechat:ua.match (/micromessenger/i) = = "Micromessenger",//
Weibo:ua.match (/weibo/i) = = "Weibo",//Weibo
Qq:ua.match (/qq/i) = = "QQ"//qq
};
}(),
Language: (Navigator.browserlanguage | | navigator.language). toLowerCase ()
};
Console.log (navigator.useragent);
Service-Side judgment

Nodejs is also judged by useragent, the code is as follows:


var browser= function (req) {
var u = req.headers[' user-agent '];
var ua = U.tolowercase ();
Mobile Terminal Browser version information
return {
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.*/),//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,//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
Wechat:ua.match (/micromessenger/i) = = "Micromessenger",//
Weibo:ua.match (/weibo/i) = = "Weibo",//Weibo
Qq:ua.match (/qq/i) = = "QQ"//qq space
};
};

Exports.browser=browser;
Basic judgments have, can be based on the results of the decision to give a different response ~

Use JavaScript to determine if a user is a mobile device

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.