Detect mobile user agent from classic ASP and redirect on session start

I was looking for a way to do this myself. After taking the code already here I found a few issues (nothing special, just probably mixing languages, a thing I do regularly). Here's the altered version corrected for Classic ASP.

 Function Is_Mobile()
  Set Regex = New RegExp
  With Regex
    .Pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|ipad)"
    .IgnoreCase = True
    .Global = True
  End With
  Match = Regex.test(Request.ServerVariables("HTTP_USER_AGENT"))
  If Match then
    Is_Mobile = True
  Else
    Is_Mobile = False
  End If
End Function

Notice I didn't declare the two variables, I know it's lazy but as ASP isn't Option Explicit I find it a useful convenience.

This is now working like a charm on my page for mobile detection, as follows:

<%If Is_Mobile() then%>
  <META NAME="viewport" CONTENT="initial-scale = 0.6, user-scalable = no">
  <LINK REL="stylesheet" TYPE="text/css" HREF="/CSS/Mobile.css" />
<%Else%>
  <LINK REL="stylesheet" TYPE="text/css" HREF="CSS/Default.css" />
<%End If%>

Hope that helps.


Take a look at:

http://mobiforge.com/developing/story/lightweight-device-detection-asp

sub is_mobile()
  Dim Regex, match
  Set Regex = New RegExp
     With Regex
        .Pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|ipad)"
        .IgnoreCase = True
        .Global = True
      End With
   match = Regex.test(Request.ServerVariables("HTTP_USER_AGENT"))
   If match Then
      return True
   Else
      return False
   End If
End Sub

*Disclaimer: the code may not work, as I have no method to test it and little knowledge of classic ASP.


Updated for android support

Function is_mobile()
  Dim Regex, match
  Set Regex = New RegExp
     With Regex
        .Pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|ipad|Android|BlackBerry|iPhone|iPod|Palm|Symbian)"
        .IgnoreCase = True
        .Global = True
      End With
   match = Regex.test(Request.ServerVariables("HTTP_USER_AGENT"))
   If match then
      is_mobile=True
   Else
      is_mobile=False
   End If
End Function