It can often happen that you need to know some informations from the browser or the client that sent the request, in order to make specific decisions. The request is from a mobile device? Or from Firefox? Or maybe from Internet Explorer?

If you ever needed to have an answer to these questions, let's know that Liferay has a very useful class for this purpose; let's see what it is.

This class is called com.liferay.portal.kernel.servlet.BrowserSnifferUtil and it has many methods for all (or almost) issues:

  • acceptsGzip(HttpServletRequest)
  • isAndroid(HttpServletRequest)
  • isChrome(HttpServletRequest)
  • isFirefox(HttpServletRequest)
  • isIe(HttpServletRequest)
  • isIphone(HttpServletRequest)
  • isLinux(HttpServletRequest)
  • isMac(HttpServletRequest)
  • isMobile(HttpServletRequest)
  • isWindows(HttpServletRequest)
  • ...

Each of these methods is quite obvious and can be invoked everywhere, both inside a Java class or inside a JSP, and can be very useful to follow different ways depending on the device.

But what if I need the same information also within JavaScript? That's not a problem because, even now, Liferay provides the JavaScript object corresponding to the BrowserSnifferUtil class:

  • Liferay.Browser.acceptsGzip()
  • Liferay.Browser.isAndroid()
  • Liferay.Browser.isChrome()
  • Liferay.Browser.isFirefox()
  • Liferay.Browser.isIe()
  • Liferay.Browser.isIphone()
  • Liferay.Browser.isLinux()
  • Liferay.Browser.isMac()
  • Liferay.Browser.isMobile()
  • Liferay.Browser.isWindows()
  • ...