Home > Web Front-end > JS Tutorial > How does js determine whether the user is using WeChat browser_javascript skills

How does js determine whether the user is using WeChat browser_javascript skills

WBOY
Release: 2016-05-16 16:45:53
Original
1239 people have browsed it

I received a request last week. The request is as follows: when the user scans the QR code, a link will be generated. The link will send a request to the backend and return an apk download address. The user can click the download button to download the apk. Then a problem occurred. After testing, it was found that the apk could not be downloaded by clicking the download button on the page opened by scanning WeChat. After Baidu found out, it turned out that the built-in browser of WeChat blocked the download link. After communicating with the demander, the demand was changed to if If the user opens the page using WeChat's built-in browser, the user is prompted to change the browser to open the page, otherwise the apk cannot be downloaded. So how to determine whether the user is using WeChat browser?

We know that js can obtain browser-related information through window.navigator.userAgent, such as: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/ 537.36, then we can also use this method to obtain relevant information about WeChat’s built-in browser: Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11d201 MicroMessenger/ 5.3. Use the keyword MicroMessenger to determine whether it is WeChat’s built-in browser. The judgment function is as follows:

Copy code The code is as follows:

function isWeiXin(){
var ua = window.navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i) == 'micromessenger'){
return true;
}else{
return false;
}
}

demo:
Copy code The code is as follows:




Determine whether it is WeChat’s built-in browser


You can view it if you open it with WeChat browser to the following text







Note: You can put the above demo on the server, and then generate a QR code to scan.
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