获取网页是手机端还是PC端访问

时间:2023-01-25 02:49:07

C#方式:

/// <summary>
/// 是否手机访问
/// </summary>
/// <returns></returns>
public static bool IsMobileBrowse()
{
string serverVariables = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
return Regex.IsMatch(serverVariables, @"Android|webOS|iPhone|iPod|BlackBerry", RegexOptions.IgnoreCase);
}

js方式:

<script type="text/javascript">
if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
     //手机端
$.get(url, function (data) {
$("#header").html(data);
});
} else {
     //PC端
$.get(url, function (data) {
$("#header").html(data);
});
}
</script>
<div id="header"></div>