This article is an example of how JS determines whether the current page is open in a micro-trust browser. Share to everyone for your reference, specific as follows:
Recently do a lot of HTML5 projects, many pages will be through micro-credit microblogging and other SNS to share out. Provide a download of the company app on the sharing page. However, in many applications of the browser, click on the download link can not download the application. So for these browsers we need to give users tips from safari or the system from the browser to open the sharing page. Through JS can determine the current page is in what browser open.
The following is a sample code, which shows how to determine whether to open in a micro-letter browser, whether in the QQ space browser or not on Sina Weibo. Of course you can do a bit better, plus the judgment is on the mobile device open or on the PC side of the browser open, more subdivision, can be judged in Android browser Open or iOS system browser open.
if (browser.versions.mobile) {//Determine if the mobile device is open. Browser code below
var ua = Navigator.userAgent.toLowerCase ();//Get the object to be judged
if (ua.match (/micromessenger/i) = = " Micromessenger ") {
//open in micro-letter
}
if (Ua.match (/weibo/i) = =" WeiBo ") {
//on Sina Weibo client open
}
if ( Ua.match (/qq/i) = = "QQ") {
//in QQ space open
} if
(Browser.versions.ios) {
//whether in the iOS browser open
}
if ( browser.versions.android) {
//whether to open in Android browser}
else {
//Otherwise it is PC browser open
}
Attach the browser code and use the following methods to determine many browsers. Including the judge IE browser, Opera browser, Apple browser, Google browser, Firefox browser and so on.
var browser = {
versions:function () {
var u = navigator.useragent, app = navigator.appversion;
return { //Mobile terminal browser version information
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.*/),//Whether for 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 the IPad
webApp: U.indexof (' Safari ') = = 1/Whether the Web should program, without head and bottom
};
} (),
language: (navigator.browserlanguage | | navigator.language). toLowerCase ()
}
I hope this article will help you with JavaScript programming.