Hi,
I wrote a simple plugin named “Browser Sniff” which allows to find browser name and version.
Using it you can write the code such as this:
{exp:browser_sniff}
{if browser_name!="ie"}
Some code
{if:else}
Some other code
{/if}
{/exp:browser_sniff}
Or you can write code such as this:
{exp:browser_sniff}
{if browser_name=="ie" AND browser_version<=6}
Some code
{if:else}
Some other code
{/if}
{/exp:browser_sniff}
You can also use {browser_name} and {browser_version} inside the tags to output the detected name and version:
{if browser_name=='ie' AND browser_version<=6}
Browser name: {browser_name}<br>
Browser version: {browser_version}
{/if}
You can use {browser_name} and {browser_version} variables both inside and outside conditionals:
{exp:browser_sniff}
{if browser_name!="ie" AND browser_version<7}
Browser name: {browser_name}<br>
Browser version: {browser_version}
{/if}
{/exp:browser_sniff}
Possible values of {browser_name} variable are: ie (internet exploder), op (opera), konq (konqueror), saf (safari), moz (Gecko browsers), ns4 (mozilla/4), other (other browsers).
Notice about version numbers: for Gecko based browsers this plugin outputs Gecko version number and for Safari - Webkit version number (On Safari and Webkit version numbers see here).
Is there any need for such code and such plugin?
It seems to me that possibility to write such code may be handy if you want to pass some piece of CSS or HTML to some browsers and another set to other browswers (most probably there will be a need to differentiate between Internet Explorer and other browsers). Although many notable persons say that browser detection is obsolete technique I constantly find nothing better than employing it. Perhaps browser detection and forking of code will be obsolete when Microsoft will release browser better than IE7 and IE6 and IE7 will be dead, but it is very long way to go.
If there is a need for browser detection, is there a need to wrap Browser detection function into plugin? Why not using PHP function directly in templates?
I have tried this and got very strange effects: the function which now wrapped in plugin works correctly did not work as plain PHP in stylesheet templates. There is a thread about it.
Credits: Harald Hope, author of Lightweight PHP Browser Detector Script, which is used in Browser Sniff plugin;
Timon Royer (eexperience), dialog@timon-royer.com
who added support for the use of variables {browser_name} and {browser_version} outside conditionals.
Any comments and suggestions appreciated.
Changelog:
-1.2 //bug fixed - PHP error message saying that browser_version variable is undefined being displayed when outputted browser name is “other”.
-1.1.9 // added support for the use of variables {browser_name} and {browser_version} outside conditionals.
-1.1.8 // First release.
