Shopping Basket

Cart empty

Popular Items

Find Us Elsewhere

Browser Detection in Joomla 3

It is often the case that as web developers we need to add some extra coding that will deal with the quirks of particular web browsers . Using Joomla we can take advantage of Joomla's built in browser detection.

3 years ago I published a similar article which covered browser detection in Joomla 1.5 and 2.5. Things have moved on since then of course and the purpose of this revised article is to cover browser detection in Joomla 3.

The History of Browser Detection in Joomla

Browser detection is handled through the JBrowser class. This api has had a rather checkered Joomla history. In Joomla 2.5 the entire API was deprecated and looked as if it was going to be dropped and replaced with something else. Then it became mysteriously undeprecated again with the release of Joomla 3.0. For once it seems that sanity has prevailed and a preference for consistency has won out over the normal Joomla practice of change for change's sake and pointlessly changing the names of functions and/or dropping classes altogether and then replacing them with something quite similar but which is called something different.

In fact relatively little has changed with the JBrowser classes, the main and most useful functions remain. The main differences are that support for browser quirks has been dropped (probably a sensible change) and support for mobile browser detection has been beefed up.

Using JBrowser

The JBrowser class is located in Joomla's class library, in the environment subpackage. In order to use it you will first need to import it:-


Then you need to get an instance of the browser object

$browser = &JBrowser::getInstance();

You can then find information about the web browser being used through examing the properties of this object, which are accessed through the appropriate 'get' method. The methods include:-

  1. getPlatform: returns the browser platform ('win','mac' or 'unix');
  2. getBrowser: returns the browser type ('opera','chrome','palm','msie', 'amaya', 'fresco', 'avantgo', 'safari','konqueror', 'mozilla', 'lynx', 'links', 'hotjava', 'up','xiino', 'palmscape', 'nokia', 'ericsson', 'wap', 'imode', 'blackberry', 'motorola', 'mml');
  3. getMajor: returns the major version number;
  4. getMinor: returns the minor version number.

To give an example, a very common application is that you wish to load a separate stylesheet for users of Internet Explorer 6, to deal with its annoying quirks.


        $doc =& JFactory::getDocument();
        $browser = &JBrowser::getInstance();
        $browserType = $browser->getBrowser();
        $browserVersion = $browser->getMajor();
        if(($browserType == 'msie') && ($browserVersion < 7))
           $doc->addStyleSheet( 'css/ie6.css' );

Other useful methods are:-

  1. isRobot(): returns true if the user agent is in fact a robot;
  2. isSSLConnection(): returns true if the connection is SSL;
  3. isMobile(): returns true if using a mobile device;
  4. getAgentString(): returns the complete user agent string (useful for detecting specific robots, see below)


The browser object uses the reported user agent to detect this information, this information is under the control of the client so there is no guarantee that it is true. In particular you need to be careful using the hasFeature() method. For example the reported value for hasFeature('javascript') does not take account of the fact that users can choose to disable scripting on a browser that will support javascript.


JBrowser can detect the following robots:-

/* The rest alphabetically. */
'Ask Jeeves',
'FDSE robot',

There is not a direct method of determining which robot is involved. To do this you will need to get the user agent string and parse this yourself to find a particular robot, eg to find out if it is Googlebot:-

        $browser = &JBrowser::getInstance();
        $agent = $browser->getAgentString();
		 if (strpos($agent, 'Googlebot') !== false)
				$googlebot = true;


An interesting thing is that Joomla has another browser detection class, JApplicationWebClient. This class is used by the Joomla application to detect the user agent. JBrowser is probably of more interest to web designers but JApplicationWebClient is there and can be used. You create a new instance:-

       $client = new JApplicationWebClient();

JApplicationWebClient supports the following properties:-

  • mobile (boolean value, true if it is a mobile device)
  • engine (takes the values TRIDENT, WEBKIT, GECKO, PRESTO, KHTML, AMAYA)
  • browser (takes the values IE, FIREFOX, CHROME, SAFARI, OPERA)
  • browserVersion (the version number if the browser has been detected)
  • languages (the web client's accepted languages string)
  • encodings (the web client's accepted encoding string)
  • robot (boolean value, true if it is a robot)

There are several points to note. Firstly the mobile and robot properties will not necessarily give the same answers as for JBrowser, because the detection methods are different. For robots, JApplicationWebClient searches the user agent for any of the strings http|bot|robot|spider|crawler|curl|, whereas JBrowser searches for specific known bots. The mobile property will be true if the platform property as listed above is a mobile phone (eg WINDOWS_PHONE, IPHONE)

So to detect if we have a robot:-

       $client = new JApplicationWebClient();
         //do something robot-specific

The values for platform, engine and browser are defined as class constants so are referred to using the JApplicationWebClient prefix. As an example of usage, suppose we want to do something windows-specific:-

       $client = new JApplicationWebClient();
       if($client->platform == JApplicationWebClient::WINDOWS)
          //do something windows-specific

You will notice that JApplication returns more detailed information about the platform but much less about the specific browser. Hence JBrowser is probably of more use to web designers. Whether you use JBrowser or JApplicationWebClient depends on the requirements of your project. It's up to you.


Author Profile