Home >Web Front-end >JS Tutorial >How to determine WeChat's built-in browser (implemented through User Agent)_javascript skills

How to determine WeChat's built-in browser (implemented through User Agent)_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:38:111495browse

When developing WeChat public accounts, a large part of it is the development of microsites. We need to know that the current browser is WeChat’s built-in browser, so how to judge?

User Agent of WeChat’s built-in browser

How to determine the WeChat built-in browser, you first need to obtain the User Agent of the WeChat built-in browser. After testing the WeChat browser on the iPhone, its User Agent is:

Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/5.0.1

So by identifying the keyword MicroMessenger, you can determine whether WeChat has a built-in browser.

Judge via JavaScript

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

Judge via PHP

function is_weixin(){ 
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
return true;
} 
return false;
}
Statement:
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