Let me tell you the principle of implementation first
Judging whether there is a keyword micromessenger in UA, if so, it is a built-in browser for WeChat
The implementation code is as follows:
//Judge whether to log in to WeChat function isWeiXin() {var ua = window.navigator.userAgent.toLowerCase();console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1if (ua.match(/MicroMessenger/i) == 'micromessenger') {return true;} else {return false;}}if(isWeiXin()){console.log(" is from the WeChat built-in browser")}else{console.log("not from the WeChat built-in browser")}UA Ritsuko from WeChat browser
Under iPhone:
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2
Under Android:
Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255
Create a new simulator and replace UA as shown in the figure:
Simulation test with Chrome's iPhone 5
PS: js determines whether it is open in WeChat browser
Use JS to judge, and after searching for information, the effect was finally achieved. I directly uploaded the code
function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/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; }