Home > Web Front-end > JS Tutorial > JS method to determine whether the current page is opened in WeChat browser_javascript skills

JS method to determine whether the current page is opened in WeChat browser_javascript skills

WBOY
Release: 2016-05-16 15:27:01
Original
1260 people have browsed it

The example in this article describes the JS method to determine whether the current page is opened in the WeChat browser. Share it with everyone for your reference, the details are as follows:

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

The following is a sample code. The comments indicate how to use JS to determine whether it is opened in the WeChat browser, whether it is opened in the QQ Space browser, and whether it is opened in Sina Weibo. Of course, it can be made more perfect. In addition, it can be further broken down to determine whether it is opened on a mobile device or a PC browser. It can be further broken down to determine whether it is opened on an Android system browser or an IOS system browser.

if (browser.versions.mobile) {//判断是否是移动设备打开。browser代码在下面
  var ua = navigator.userAgent.toLowerCase();//获取判断用的对象
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    //在微信中打开
  }
  if (ua.match(/WeiBo/i) == "weibo") {
    //在新浪微博客户端打开
  }
  if (ua.match(/QQ/i) == "qq") {
    //在QQ空间打开
  }
  if (browser.versions.ios) {
    //是否在IOS浏览器打开
  } 
  if(browser.versions.android){
    //是否在安卓浏览器打开
  }
} else {
  //否则就是PC浏览器打开
}

Copy after login

Attach the browser code and you can determine many browsers through the following methods. Including judging IE browser, Opera browser, Safari, Google Chrome, Firefox, etc.

var browser = {
 versions: function () {
  var u = navigator.userAgent, app = navigator.appVersion;
  return {   //移动终端浏览器版本信息
   trident: u.indexOf('Trident') > -1, //IE内核
   presto: u.indexOf('Presto') > -1, //opera内核
   webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
   gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
   mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
   ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
   android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
   iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
   iPad: u.indexOf('iPad') > -1, //是否iPad
   webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
  };
 }(),
 language: (navigator.browserLanguage || navigator.language).toLowerCase()
}

Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template