识别手机浏览器代码【C#和JS两种语言】

时间:2021-04-09 22:17:37

C# 识别手机浏览器代码:

public static bool MobileBrowserDetect()
{
bool bismobile = false;
try
{ #region 包含android、iphone、微信特征 micromessenger 的,才认为是手机浏览器,判断条件简单,可以识别大部分智能浏览器,事半功倍,有点类似乐观锁
string user_agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
//移动浏览器的UA特征串
string[] mobileBrowserUA_FeatureArray = new string[] { "android", "iphone", "micromessenger" };
if (user_agent != null)
{
user_agent = user_agent.ToLower(); foreach (var uaFeature in mobileBrowserUA_FeatureArray)
{
if (user_agent.IndexOf(uaFeature) >= )
{
bismobile = true;
break;
}
}
}
#endregion
}
catch (Exception ex)
{
//
}
return bismobile;
}

JS识别手机浏览器代码:

function MobileBrowserDetech() {
var isMobile=false;
try {
var ua = navigator.userAgent;
ua = ua.toLowerCase();
var mobileUA_Feature=["android", "iphone", "micromessenger"];
for(var i=;i<mobileUA_Feature.length;i++)
{
if (ua.indexOf(mobileUA_Feature[i]) > - ) {
isMobile=true;
break;
}
} } catch(c) {}
return isMobile;
};