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) >= 0)
                        {
                            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=0;i<mobileUA_Feature.length;i++)
        {
            if (ua.indexOf(mobileUA_Feature[i]) > -1 ) {
                isMobile=true;
                break;
            }
        }
        
    } catch(c) {}
    return isMobile;
};

 

posted on 2018-12-20 10:48  踏歌&而行  阅读(938)  评论(0编辑  收藏  举报