Code Snippet
Add Data Attribute of User Agent
var b = document.documentElement;
b.className = b.className.replace('no-js','js');
b.setAttribute("data-useragent", navigator.userAgent);
b.setAttribute("data-platform", navigator.platform );Which results in data attributes being added to the html element like:
<html
data-useragent="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)"
data-platform="Win32">
...Which allows you to be able to target very specific browsers on very specific platforms with CSS:
html[data-useragent*="Chrome/13.0"][data-platform="Win32"] {
...
}
This one is very useful!
Damn, that is helpfull. And it’s a hell lot better SEO than the IE specific if statements in your HTML markup. The only thing you need to keep in mind is that this will only work on CSS that gets loaded after that specific bit of JavaScript runs. It’s also not completely bulletproof, since there’s no solid fallback or browsers that don’t support data attributes, although you might be forcing them on the elements this way, even in older browsers.
What Googlebot should see when he looks at this page:
What your CSS gets to see is the same as Chris shows above.
WTF. Don’t use user-agent sniffing when feature testing can be used instead.
Read Rogie’s article at the Reference URL. Feature testing, while obviously better, wouldn’t have helped him there.
It is really very useful. Is that CSS will work in all browsers?
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.