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.
Browser detection is handled through the JBrowser class. The 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' );
}
If there is a particular quirk that you wish to deal with, such as lack of support for alpha transparency in png images (a common complaint with IE6), you can use the getQuirks() method:-
If($browser->getQuirks('png_transparency')) { $doc->addScript( 'js/pngfix.js' ); }
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.