Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'd like to detect a mobile user agent and redirect them when the session starts in a classic ASP app. Does anyone know a good way to pull this off?

like image 654
Keith Adler Avatar asked May 14 '10 17:05

Keith Adler


1 Answers

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.

like image 82
DIsFanJen Avatar answered Sep 19 '22 15:09

DIsFanJen