X

Detect Browser

If you are using different stylesheets for different browsers theres a different way of detecting the users browsers than the HTML conditional-tags.

Now a class with the name of the browser will be added to your body-tag. Like this:

Code Snippet:

<body class="home blog logged-in ie">

How to use it?

Code Snippet:

body.ie #mydiv {<br />
  /* Here goes IE fix */<br />
}<br />

Code Snippet:

<?php
add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
	global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;
	if($is_lynx) $classes&#91;&#93; = 'lynx';
	elseif($is_gecko) $classes&#91;&#93; = 'gecko';
	elseif($is_opera) $classes&#91;&#93; = 'opera';
	elseif($is_NS4) $classes&#91;&#93; = 'ns4';
	elseif($is_safari) $classes&#91;&#93; = 'safari';
	elseif($is_chrome) $classes&#91;&#93; = 'chrome';
	elseif($is_IE) $classes&#91;&#93; = 'ie';
	else $classes&#91;&#93; = 'unknown';
	if($is_iphone) $classes&#91;&#93; = 'iphone';
	return $classes;
}
?>

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!