SoFunction
Updated on 2025-02-28

How to determine which browser to open the current page

I have been doing a lot of HTML5 projects recently, and many pages will be shared through SNS such as WeChat and Weibo. Provide downloads of the company's APP on the sharing page. However, in many browsers of applications, clicking on the download link cannot download the application. So for these browsers, we need to give users a prompt to open the sharing page from safari or the browser provided by the system. Through js, we can determine which browser the current page is opened in.

The following is a sample code. The comment shows how to determine whether it is open in the WeChat browser and whether it is open in the WeChat browser through JS.QQ Space Browser, is it thereSina Weibo opens. Of course, it can be done more thoroughly, and the judgment ismobile deviceOpen andYesPC browserOpenYes, you can refer to this article for a more detailed breakdown, and you can judge that it isAndroid browserOpenStillIOS system browserOpenof.

if () {//Discern whether the mobile device is on.  Browser code is below    var ua = ();//Get the object for judgment    if ((/MicroMessenger/i) == "micromessenger") {
        //Open in WeChat    }
    if ((/WeiBo/i) == "weibo") {
        //Open on Sina Weibo client    }
    if ((/QQ/i) == "qq") {
        //Open in QQ space    }
    if () {
        //Is it open in IOS browser    } 
    if(){
        //Is it open in Android browser?    }
} else {
    // Otherwise, it will be opened by the PC browser}

Then attach the browser code, and you can judge many browsers through the following methods. Including judging IE browser, Opera browser, Safari browser, Google browser, Firefox browser, etc.

var browser = {
  versions: function () {
    var u = , app = ;
    return {     //Mobile terminal browser version information      trident: ('Trident') > -1, //IE kernel      presto: ('Presto') > -1, //opera kernel      webKit: ('AppleWebKit') > -1, //Apple, Google kernel      gecko: ('Gecko') > -1 && ('KHTML') == -1, //Firefox core      mobile: !!(/AppleWebKit.*Mobile.*/), //Is it a mobile terminal      ios: !!(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios terminal      android: ('Android') > -1 || ('Linux') > -1, //Android terminal or uc browser      iPhone: ('iPhone') > -1, //Is it an iPhone or QQHD browser?      iPad: ('iPad') > -1, //Is it an iPad      webApp: ('Safari') == -1 //Whether the web should be a program, there is no header or bottom    };
  }(),
  language: ( || ).toLowerCase()
}

Another way:

Use JS to judge, and after searching for information, the effect was finally achieved. I directly uploaded the code

function is_weixn(){ 
  var ua = (); 
  if((/MicroMessenger/i)=="micromessenger") { 
    return true; 
  } else { 
    return false; 
  } 
} 

Passed the test completely, whether it is Android or iPhone or iPad. Of course, in addition to using js to judge, it is easier to judge in other languages, such as PHP

function is_weixin(){ 
  if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) { 
      return true; 
  }  
  return false; 
} 

The above is to share with you the method of determining which browser to open the current page by js. I hope it will be helpful to everyone's learning.