如何判断当前页面是否在微信浏览器中打开

JavaScript015

如何判断当前页面是否在微信浏览器中打开,第1张

方法一:使用JS判断。

functionis_weixn(){

varua=navigator.userAgent.toLowerCase()

if(ua.match(/MicroMessenger/i)=="micromessenger"){

returntrue

}else{

returnfalse

}

}

方法二:使用PHP判断。

functionis_weixin(){

if(strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')!==false){ //strpos()函数查找字符串在另一字符串中第一次出现的位置。

returntrue

}

returnfalse

}

js判断页面是否关闭、刷新或跳转的方法:

window.onbeforeunload=function (){

alert("===onbeforeunload===")

if(event.clientX>document.body.clientWidth &&event.clientY <0 || event.altKey){

alert("关闭了浏览器")

}else{

alert("正在刷新页面")

}

}

这段代码就是判断触发onbeforeunload事件时,鼠标是否点击了关闭按钮,或者按了ALT+F4来关闭网页,如果是,则认为系统是关闭网页,否则在认为系统是刷新网页。