Detect if page is from a mobile device?
How do i know if my webpage (asp.net) is being loaded from a mobile device? I want to apply mobile.css instead of normal.css. I consider a mobile device all phones, psp/ds/wii, itouch and ipad could go either way.
I dont know if i am choosing the css by headers serve开发者_高级运维r side changing the css file or if i should use JS or something. How do i do this?
I think this .net api, 51degrees, is the best for WURFL.
Check the UserAgent to see what device is requesting your page, e.g.:
http://www.codeproject.com/KB/aspnet/conquering_browsers.aspx
Google knows more.
Any library that you might use to detect whether the site is being accessed by a mobile device would ultimately, in most probability pick up the User Agent string and then compare it with a device data store ( like WURFL or .Mobi ).
There are several open source libraries like Tera_WURFL but I am not sure what is available for .Net applications.
One thing that we did in one of the projects that I worked with was instead of checking for all the mobile User Agents and defaulting to desktop if not found, we did the other way around. We check for all the desktop user agents and defaulted to mobile if not found. I feel this improves the performance as there are reduced number of comparisons.
You could detect it on server side by looking into UserAgent
property of the Request
object. That should work with most mobile browser but will require writing a couple of "or" statements.
For example to detect an iPhone you can use something like this
if (Request.UserAgent.Contains("iPhone")) {
// load iphone specific JS/CSS here
}
I think this is the best way of putting a "check if mobile" code snippet. This website lets you download the code snippet in your preferred language of choice.
It has almost ALL of the mobile user agents.
http://detectmobilebrowsers.com/
精彩评论