JS method to determine whether the current page is opened in WeChat browser _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to determine whether JavaScript can open the current page in a browser. it involves JavaScript client-side judgment techniques and is very simple and practical, for more information about how to use JavaScript to determine whether the current page is opened in a browser, see the example in this article. We will share this with you for your reference. The details are as follows:

Recently I have made many HTML5 projects, and 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. Through js, you can determine the browser in which the current page is opened.

The following is an example of code. the comment shows how to determine whether to open in the browser, whether to open in the QQ space browser, and whether to open in Sina Weibo through JS. Of course, you can do a better job. In addition, you can determine whether to enable it on a mobile device or in a PC browser, it can be determined whether it is opened in the Android browser or in the IOS browser.

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 ()}

I hope this article will help you design JavaScript programs.

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.