Skip to content

galori/simple-useragent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-useragent

The useragent gem provides a few utilities related to browser detection. It has two main uses, described below:

  1. The “somewhat less hacky CSS browser targetting”

  2. Mobile / Desktop / iPhone / iPad / Blackberry detection

1. The “somewhat less hacky CSS browser targetting”

Admittedly targeting CSS to a specific browser is a bad practice that should generally be avoided. For more details about why browser detection in general is a bad idea, see www.quirksmode.org/js/support.html. However sometimes there is no choice - especially under time constraints.

Add to your body tag:

<body class='<%=SimpleUserAgent::browser(request)%>'>

This will add one of (ie6 | ie7 | ie8 | firefox | safari | chrome) as a class to your body tag. Then, instead of using the classic browser specific CSS hacks such as:

/* IE7 */
*:first-child+html #content { 
 height: 500px;
}

now you can do this:

.ie7 #content { 
  height: 500px;
}

Which is both cleaner, and self documenting.

2. Mobile / Desktop / iPhone / iPad / Blackberry detection

The following new methods are available to you:

SimpleUserAgent::is_desktop?
SimpleUserAgent::is_mobile?
SimpleUserAgent::is_blackberry?
SimpleUserAgent::is_iphone?
SimpleUserAgent::is_apple?
SimpleUserAgent::is_ie?   
SimpleUserAgent::is_ipad?

The is_mobile? and is_desktop? methods are using a minimalistic approach, converted from the python script here: gist.github.com/88057

The theory is that since there are a lot more mobile user agents than there are desktop user agents (hundreds or even thousands) - we detect the known list of desktop user agents, and if its not one of those we assume that it is mobile. It works surprisingly well in the vast majority of cases. So far I’ve only seen it fail with spoofed or malformed user agents.

Copyright © 2009 Gal Steinitz. See LICENSE for details.

About

provides a few utilities related to browser detection

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages