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.
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.
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:-
jimport('joomla.environment.browser');
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:-
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.
Example
jimport('joomla.environment.browser'); $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:-
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:-
'Googlebot'
'msnbot',
'Slurp',
'Yahoo',
/* The rest alphabetically. */
'Arachnoidea',
'ArchitextSpider',
'Ask Jeeves',
'B-l-i-t-z-Bot',
'Baiduspider',
'BecomeBot',
'cfetch',
'ConveraCrawler',
'ExtractorPro',
'FAST-WebCrawler',
'FDSE robot',
'fido',
'geckobot',
'Gigabot',
'Girafabot',
'grub-client',
'Gulliver',
'HTTrack',
'ia_archiver',
'InfoSeek',
'kinjabot',
'KIT-Fireball',
'larbin',
'LEIA',
'lmspider',
'Lycos_Spider',
'Mediapartners-Google',
'MuscatFerret',
'NaverBot',
'OmniExplorer_Bot',
'polybot',
'Pompos',
'Scooter',
'Teoma',
'TheSuBot',
'TurnitinBot',
'Ultraseek',
'ViolaBot',
'webbandit',
'www.almaden.ibm.com/cs/crawler',
'ZyBorg'
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:-
jimport('joomla.environment.browser'); $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:-
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(); if($client->robot) { //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.