This article mainly introduces how to use js to determine which browser to open the current page. For more information, see some HTML5 projects recently. Many pages will be shared through SNS such as Weibo. Download the company APP on the sharing page. However, in the browsers of many applications, you cannot download the application by clicking the download link. For these browsers, We Need To prompt users to open the sharing page from safari or the browser that comes with the system. Then, we can use js to determine the browser in which the current page is opened.
The following is a sample code. The comment shows how to determine whether to open in the browser or not through JS.QQ space Browser, WhetherSina Weibo opens. Of course, we can do a better job.Mobile DevicesOpen or inPC browserOpen, this can be referred to this article, a more detailed point, you can judge is inAndroid BrowserOpen orIOS BrowserOpen.
If (browser. versions. mobile) {// determines whether the mobile device is enabled. The browser code is as follows: var ua = navigator. userAgent. toLowerCase (); // obtain the object if (ua. match (/MicroMessenger/I) = "micromessenger") {// open in} if (ua. match (/WeiBo/I) = "weibo") {// open it on the Sina WeiBo client} if (ua. match (/QQ/I) = "qq") {// open in QQ space} if (browser. versions. ios) {// whether to open in IOS browser} if (browser. versions. android) {// whether to open in the android browser} else {// otherwise, it is opened in the PC browser}
Attach the browser code and use the following method to determine many browsers. Including IE browser, operabrowser, Apple browser, Google browser, Firefox browser, etc.
Var browser = {versions: function () {var u = navigator. userAgent, app = navigator. appVersion; return {// mobile terminal browser version information trident: u. indexOf ('think')>-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 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 the browser is iPhone or QQHD iPad: u. indexOf ('ipad ')>-1, // whether iPad webApp: u. indexOf ('safari ') =-1 // whether the web should program, no header and bottom};} (), language: (navigator. browserLanguage | navigator. language ). toLowerCase ()}
Another method:
It was judged by JS. After searching for information, the results were finally achieved and the code was directly uploaded.
function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)=="micromessenger") { return true; } else { return false; } }
The test is complete. Both android and iphone and ipad can be used. Of course, in addition to js, it is easier to use other languages for judgment, such as PHP.
function is_weixin(){ if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) { return true; } return false; }
The above shows how to use js to determine which browser to open the current page. I hope this will help you in your learning.