TRS Design Blog

Design & Code Blog

Blah, Blah, Blah, Blog…

Mobile detection in Classic ASP

At TRS Design we love classic ASP, most of our sites are classic ASP – yes it’s a little old now but it’s powerful, it’s secure and it’s stable. It allows us to write great sites that always work for our clients! We’re fans.

In this post we’re sharing how we do mobile device detection, whether it’s phone or tablet, Apple or Android, we basically pass through an array of devices to see if there’s a match, if there is we set a variable, session or cookie, else we just display the page. You can also override the detection by checking the user has not specifically selected they want to view the site in full mode, not mobile version. Cool, eh?

The Code:
' ========================

Dim user_agent, mobile_browser, Regex, match, mobile_agents, mobile_ua, i, size
user_agent = Request.ServerVariables("HTTP_USER_AGENT")
mobile_browser = 0
Set Regex = New RegExp
With Regex
.Pattern = "(up.browser||mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm)"
.IgnoreCase = True
.Global = True
End With
match = Regex.Test(user_agent)
If match Then mobile_browser = mobile_browser+1
If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/vnd.wap.xhtml+xml") Or Not IsEmpty(Request.ServerVariables("HTTP_X_PROFILE")) Or Not IsEmpty(Request.ServerVariables("HTTP_PROFILE")) Then
mobile_browser = mobile_browser+1
end If
' // Now you're going through the list of devices,
' // this is an array so as time moves on, just add
' // more that come to the marketplace into here
mobile_agents = Array("alcatel", "amoi", "android", "avantgo", "blackberry", "benq", "cell", "cricket", "docomo", "elaine", "htc", "iemobile", "iphone", "ipad", "ipaq", "ipod", "j2me", "java", "midp", "mini", "mmp", "mobi", "motorola", "nec-", "nokia", "palm", "panasonic", "philips", "phone", "sagem", "sharp", "sie-", "smartphone", "sony", "symbian", "t-mobile", "telus", "up\.browser", "up\.link", "vodafone", "wap", "webos", "wireless", "xda", "xoom", "zte")
size = Ubound(mobile_agents)
mobile_ua = LCase(Left(user_agent, 4))
' // You've previously set mobile_browser as 0,
' // now loop through the array set above and
' // if one or more is matched, add 1 to this variable
For i = 0 To size
If mobile_agents(i) = mobile_ua Then
mobile_browser = mobile_browser+1
Exit For
End If
' // Check that full website has not been requested
' // (you can have a link in the page for this like:
' // View Full Site
If request.querystring("v")="full" then session("fullsite")=true
' // Check to see if the var mobile_browser is greater
' // than 0, if so it's a mobile device, act accordingly.
If mobile_browser>0 and session("fullsite")=false then
' // redirect the user to a mobile appropriate page you've written
End If
' =======================

Categorized as Classic ASP Coding


  1. Thanks for this. I have been wondering how to accomplish mobile detection and you saved me at least a coupe hours worth of work. I hope I can help you out in the same way sometime. We are Classic ASP enthusiasts also…

  2. reginald lycke says:

    Great script !
    After testing with the script i come to the conclusion that on a ipad it showes the mobile site instead of the regular? How can i avoid this?
    greetings from belgium

  3. Hi Reginald,
    This script picks up all ‘mobile devices’, so that means pads/tablets/mobile or cell phones, web enabled small devices, basically. To differentiate between each device simply, without using large databases or external services, I recommend using this:
    I’ve tested it in ASP converting it directly from PHP and it works nicely. It’s not going to get everything right but it will be close! Tip: PHP funcion $stripos = vbscript inStr. Also, at the very end, do an ‘else’ and this would mean it’s none of the above and simply a PC/MAC or not recognised mobile device.
    I hope this helps!
    Greetings from Spain.

  4. I have created an online word challenge game using CLASSIC ASP and I would like it to be a mobile app as well. I want the game to be able to fit the size of a mobile device’s screen. You enter letters using the keyboard. Will the code above allow the game to fit the screen of a mobile device? If so, that would be great!!!

  5. Great code to redirect the user from a browser to a mobile version of the site.

    What code would I put on the mobile site to have a “View Full Site” link that will allow the user to go to the browser version, even while on a handheld device?

    I placed the above code on the browser version. The link on the mobile just gets redirected back to the mobile site homepage.

    P.S. I am a ASP novice.

  6. Hi Mike,

    You would add a querstring to your homepage like and in your homepage request the querystring, if it’s v=full then set a session variable instructing your website to not redirect, otherwise let it redirect to the mobile.

    Hope that helps

  7. Hi Bjwool,

    The code is just for detection and redirecting to the appropriate version. You’d then have to make your pages suitable for mobile content.

    There’s lots of info on the web about how to do that.

    Best of luck!

  8. Thank you so much. I was able to use this code to detect if a mobile device was present, and if so, deactivate scripts or CSS elements that do not work with mobile devices. I commented out lines 38 to 48 and wrote an if/then statement based on mobile_browser = 0. I could not have written this script myself with my level of code experience. It’s also good to know that if I develop a mobile site later I will be able to use this script to redirect users. Thanks for keeping up with ASP Classic!

  9. this code seems too work for every other device accept BlackBerry. Have they changed something?

  10. I did a test with a relatively up-to-date BlackBerry phone a few months back and it was OK. Are you using a pretty new handset or tablet or…? I also recommend using the code in comment 3 above – this might capture what you need. Good luck!

  11. I’m new to asp and mobile/full site detection. I’m having trouble implementing this. Is there any place where I can find step by step instructions on how to make this happen?

  12. HI Rebecca. I commented the code as well as I could. If you just drop it at the top of any ASP page it should either stay on that page or redirect to mobile-version.asp depending on if it’s a computer browser or a smartphone browser. I don’t know of any other step-by-step guides in ASP, sorry. I hope you figure it out.

  13. Perferct! Just what I was after so I can start to build on a mobile version of our ASP site. I’ll keeping checking back on your blog to see if you add any more great posts on classic ASP and mobile site development.

  14. Manfred Leister says:

    This is so nice of you showing us how to do this.
    I appreciate so much.
    B Rgds frolm Spain –

  15. Kathrine Rodriguez says:

    Thanks so much for this code! I have been an classic ASP programmer for many years and agree with you that it’s very stable and verstile. I have implemented this mobile checking along with browser detection and it’s working out really great!! Again, thanks so much for sharing.

  16. This is exactly what I was after and it works like a charm. Thanks for sharing.

Leave a Reply