jQUERY

mobile browser check

따랑 2015. 5. 20. 13:41

//javascript

var mobileInfo = new Array('Android', 'iPhone', 'iPod', 'BlackBerry', 'Windows CE', 'SAMSUNG', 'LG', 'MOT', 'SonyEricsson');
for (var info in mobileInfo){
    if (navigator.userAgent.match(mobileInfo[info]) != null){
        // mobile

        break;
    }
}

//jquery

var ua = window.navigator.userAgent.toLowerCase();
if ( /iphone/.test(ua) || /android/.test(ua) || /opera/.test(ua) || /bada/.test(ua) ) {
    var mobilecheck1 = getUrlVars('place');
 var mobilecheck2 = getUrlVars('num');
    if(mobilecheck2 != ""){
        window.location = "http://www.ddarang.com/mobile/view.aspx?num="+mobilecheck2;
    }
 else if(mobilecheck1 == "pc"){
        window.location = "http://www.ddarang.com";
    }
 else
 {
  window.location = "http://www.ddarang.com/mobile/default.aspx";
 }
}
function getUrlVars()
{
 var vars = [], hash;
 var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 for(var i = 0; i < hashes.length; i++)
 {
  hash = hashes[i].split('=');
  vars.push(hash[0]);
  vars[hash[0]] = hash[1];
 }
 return vars;
}

 

//php

$ary_m = array("iPhone","iPod","IPad","Android","Blackberry","SymbianOS|SCH-M\d+","Opera Mini","Windows CE","Nokia","Sony","Samsung","LGTelecom","SKT","Mobile","Phone");
  for($i=0; $i<count($ary_m); $i++){
   if(preg_match("/$ary_m[$i]/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
    return $ary_m[$i];
    break;
   }
  }
  return "PC";

//asp

Dim gMobilePhones, gUserAgent, gMobileYN, gStrAgent, mn, mUrl
If(Len(Request.ServerVariables("HTTP_USER_AGENT"))=0) Then
  gStrAgent = "NONE"
Else
  gStrAgent = Request.ServerVariables("HTTP_USER_AGENT")
End if
gMobilePhones = Array("iPhone", "ipad", "BlackBerry", "Android", "Windows CE", "LG", "MOT", "SAMSUNG", "SonyEricsson", "nokia", "webos", "opera mini", "sonyericsson", "opera mobi", "iemobile")

For mn= 0 to uBound(gMobilePhones)
 If (InStr(LCase(gStrAgent), LCase(gMobilePhones(mn))) > 0) Then
   gUserAgent = LCase(gMobilePhones(mn))
   gMobileYN = true
 End If
Next

If gMobileYN Then
 //mobile
End If

//ASP.NET

1. 이렇게 해볼까나..

string userAgent = Request.UserAgent;
string[] arrBrowser = { "iphone", "ipod", "Iemobile", "mobile", "lgtelecom", "ppc" };


for (int i = 0; i < arrBrowser.Length; i++)
{
    if (userAgent.ToLower().IndexOf(arrBrowser[i]) != -1)
        Response.Redirect("http://m.ddarang.com/");
}

 

2. 이렇게도...브라우져 종류가 많네....

string sUserAgent = Request.UserAgent.ToString().Trim();

        string[] strChekName = null;
        string sMobileStr = "Windows CE,PPC,Windows Phone,IEMobile,SKT,iPhone,lgtelecom,NATEBrowser,iPod,iPad,Nokia,BlackBerry,SonyEricsson,Android";

        strChekName = sMobileStr.Split(',');

        int n = 0;
        for (n = 0; n < strChekName.Count(); n++)
        {
            if (sUserAgent.IndexOf(strChekName[n].ToString().Trim()) > 0)
            {
                Response.Redirect("http://m.ddarang.com/");
            }
        }