C#和JQ判断移动端还是PC端
生活随笔
收集整理的這篇文章主要介紹了
C#和JQ判断移动端还是PC端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
判斷移動端還是PC端瀏覽網頁可以使用兩種方法:
1、在C#語句進行判斷
/// <summary>/// 判斷是移動端/// </summary>/// <returns></returns>public static bool IsMobileDevice(){string[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod", "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", "googlebot-mobile" };bool isMoblie = false;string userAgent = HttpContext.Current.Request.UserAgent.ToString().ToLower();//排除 Windows 桌面系統或蘋果桌面系統 if (!string.IsNullOrEmpty(userAgent) && !userAgent.Contains("macintosh") && (!userAgent.Contains("windows nt") || (userAgent.Contains("windows nt") && userAgent.Contains("compatible; msie 9.0;")))){for (int i = 0; i < mobileAgents.Length; i++){if (userAgent.ToLower().IndexOf(mobileAgents[i]) >= 0){isMoblie = true;break;}}}return isMoblie;}使用方法:
if (HelpDal.IsMobileDevice()){HttpContext.Current.Response.Redirect("index.aspx");}else{///}2、使用JQ去判斷
//判斷是移動端 function IsMobileURL(URL) {var ua = window.navigator.userAgent.toLowerCase();if ((navigator.userAgent.match(/(phone|pad|pod|mobile|ios|android|BlackBerry|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|symbian|transcoder)/i))) {window.location.href = "index.aspx";}else if (ua.match(/MicroMessenger/i) == 'micromessenger') {window.location.href = "index.aspx";} }//判斷是PC端 function IsPC(URL) {if (!(navigator.userAgent.match(/(phone|pad|pod|mobile|ios|android|BlackBerry|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|symbian|transcoder)/i))) {window.location.href = "Index.aspx";} }總結
以上是生活随笔為你收集整理的C#和JQ判断移动端还是PC端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 权益证明
- 下一篇: 台式机安装双系统win10+Ubuntu