You can detect the browsers via the JBrowser class located in Joomla's class library, in the environment subpackage. 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 by using appropriate methods. The methods include:
- getPlatform: returns the browser platform ('win','mac' or 'unix');
- getBrowser: returns the browser type ('opera','palm','msie', 'amaya', 'fresco', 'avantgo', 'konqueror', 'mozilla', 'lynx', 'links', 'hotjava', 'up','xiino', 'palmscape', 'nokia', 'ericsson', 'wap', 'imode', 'blackberry', 'motorola', 'mml');
- getMajor: returns the major version number;
- getMinor: returns the minor version number.
Here is a working 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' );
}
{
$doc->addScript( 'js/pngfix.js' );
}
Other useful methods are:-
- isRobot(): returns true if the user agent is in fact a robot;
- isSSLConnection(): returns true if the connection is SSL.
- hasFeature('feature'): returns true if the browser supports the feature 'feature', which can include among others ('iframes', 'frames', 'javascript','java', 'images', 'dom, 'svg')