Browser Detection Plugin

7 votes

3% of Grails users

Dependency:
compile ":browser-detection:2.1.0"

 Documentation  Source  Issues

Summary

This plugin helps you detect browsers, versions, language and operating systems from the request headers.

Installation

Add the dependency to the plugin in your BuildConfig.groovy. Remember to replace the "x.x.x" for the latest version.

compile ':browser-detection:x.x.x'

If you encounter a Maven repo dependency issue, try adding this line to the repositories section of your BuildConfig.groovy

mavenRepo 'https://raw.githubusercontent.com/HaraldWalker/user-agent-utils/mvn-repo/'

Description

This plugin provides a service and tag library for browser detection. It parses User-Agent in order to identify client's browser, operating system, etc. It depends on user-agent-utils library from HaraldWalker.

If you want to contribute, report issues or just check the code. You can find it at GitHub.

Here are some of the things that you can currently do with this plugin. All this operations can be used from GSPs or you can use the UserAgentIdentService anywhere in your code.

Detecting Browsers

<browser:isMsie> This is Internet Explorer </browser:isMsie>
<browser:isSafari> This is Safari </browser:isSafari>
<browser:isChrome> This is Chrome </browser:isChrome>
<browser:isFirefox> This is Firefox </browser:isFirefox>
<browser:isOpera> This is Opera </browser:isOpera>

Detecting Devices

<browser:isiPhone> This is iPhone </browser:isiPhone>
<browser:isiPad> This is iPad </browser:isiPad>
<browser:isMobile>Mobile phones or Android, iPhone, iPad, iPod, Blackberry, etc.</browser:isMobile>

Detecting Operative Systems

<browser:isWindows> This is Windows </browser:isWindows>

Other operations

You can use the following structure that emulates switch behavior:

<browser:choice>
	<browser:isIE6></browser:isIE6>
	<browser:isIE7></browser:isIE7>
	<browser:otherwise></browser:otherwise>
</browser:choice>

Or the one below:

<browser:isSafari versionGreater="5">
	This text is rendered if Safari version is greater than 5.
	For example, 5.0.1, 5.1
</browser:isSafari>
<browser:isFirefox version="3.*">
	It works for all Firefox versions like 3.1, 3.6 and so on
</browser:isFirefox>
<browser:isMsie versionLower="7">
	Internet Explorer 5.0, Internet Explorer 6.0
</browser:isMsie>

At the moment, wildcards are allowed only for version attribute. Be aware that 5.1 is greater than 5 and 5.0 equals to 5.