We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Browser Sniff

Development and Programming

Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

The attached add-on files may be out of date. Please visit Devot-ee for the latest versions.

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}, {browser_version}, {browser_subversion} and {os} variables inside the tags to output the detected browser name, version and OS:

{if browser_name=='ie' AND browser_version<=6} 
Browser name: {browser_name}

Browser version: {browser_version}

Browser subversion: {browser_subversion}

OS: {os}
{/if}

You can use {browser_name}, {browser_version}, {browser_subversion} variables both inside and outside conditionals:

{exp:browser_sniff}
{if browser_name!="ie" AND browser_version<7}
Browser name: {browser_name}

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).

Possible values of {os} variable: win (windows), mac (Macintosh), linux (Linux), other (other OS).

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), [email protected] who added support for the use of variables {browser_name} and {browser_version} outside conditionals.

Any comments and suggestions appreciated.

       
Ronin_23's avatar
Ronin_23
58 posts
17 years ago
Ronin_23's avatar Ronin_23

Nice work! I like this plugin.

Examples: Warning for old IE6

{exp:browser_sniff} {if browser_name=="ie" AND browser_version<=6} ATTENTION! This site is optimized for the actual IE, please update your browser. Microsoft Update Website/images/system/browser-ie.gif {/if} {/exp:browser_sniff}

Specific Bookmark

{exp:browser_sniff} {if browser_name=="ie"} {/if} {/exp:browser_sniff} {exp:browser_sniff} {if browser_name=="moz" OR browser_name=="ns4"} {/if} {/exp:browser_sniff}

Unfotunately I don’t know how to set an Opera Bookmark Link.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Ronin,

Should there be some code in where you have the ’s above. You mention that this is for bookmarks. Do you mean this will auto-add in a bookmark for the site? If so then I think maybe there is some code missing there. Maybe the forums here have stopped certain text from being shown?

Best wishes,

Mark

P.S. Nice plugin by the way Laisvunas, well done 😊

       
Ronin_23's avatar
Ronin_23
58 posts
17 years ago
Ronin_23's avatar Ronin_23

yep, it was browser specific add to favorites javascript code … don’t know how to post it?

       
Ronin_23's avatar
Ronin_23
58 posts
17 years ago
Ronin_23's avatar Ronin_23

the code works in the preview, and online it is deleted

replace all underlines in the link to use it

{exp:browser_sniff}
{if browser_name=="ie"}
<a href="http://ja_vascr_ipt:wind_ow.exte_rnal.A_ddFavo_rite%27url%27,">_/images/system/site_tools_bookmark.gif <strong>add to favorites</strong></a>

{/if}
{/exp:browser_sniff} 

{exp:browser_sniff}
{if browser_name=="moz" OR browser_name=="ns4"}
<a href="http://ja_vascr_ipt:win_dow.sid_ebar.a_ddPa_nel%27title%27,">_/images/system/site_tools_bookmark.gif <strong>add to bookmarks</strong></a>


{/if}
{/exp:browser_sniff}
       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

If you have any <sc*ipt type=”“> type of text then you will need to swap out one of the letters for something else or place spaces between the start and end of the tags like I have shown just now so either :

<sc*ipt type="">

or

< script type="" >

Please remove the spaces after the opening tag and before the closing tag before using this code

Something like that should hopefully do it for you?

Best wishes,

Mark

       
Ronin_23's avatar
Ronin_23
58 posts
17 years ago
Ronin_23's avatar Ronin_23

now it works, there seem to be several blockers..

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Excellent,

Thanks for the code. Yep I think the words script and javascript and perhaps a few others are blocked to stop any weird code injection going on with the forums.

Best wishes,

Mark

       
Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

Hi Ronin and Mark,

I am glad that you find this plugin useful.

Browser detection script I used in this plugin is very simple, but it seems to me that for most practical purposes it is good enough.

If someone would need more powerful browser detection, it is very easy to find more powerful function and wrap it into the plugin analogously I have done in Browser Sniff.

       
eexperience's avatar
eexperience
56 posts
17 years ago
eexperience's avatar eexperience

Hi Laisvunas, just wanted to say thank you. This is exactly what I need. I want to use the balupton jquery lightbox but only the older version works with IE6 and only the newer version looks good with IE7. Thanks for your efforts. I really appreciate your efforts, please keep up the good work!

Thanks, Timon

       
Rob Quigley's avatar
Rob Quigley
236 posts
17 years ago
Rob Quigley's avatar Rob Quigley

I assume browser version detection would only work for IE browsers, right?

{if browser_name=="ie" AND browser_version<=6}
       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

This is from the plugin notes :

Place the following tags in any of your templates:
{exp:browser_sniff}{/exp:browser_sniff}
Place between these tags browser detection code such as this:
{if browser_name=='ie' AND browser_version<=6} some code {/if}
Possible values of browser_name variable: ie (internet exploder), op (opera), konq (konqueror), saf (safari), moz (Gecko browsers), ns4 (mozilla/4), other (other browsers).

Hope that helps.

Best wishes,

Mark

       
Rob Quigley's avatar
Rob Quigley
236 posts
17 years ago
Rob Quigley's avatar Rob Quigley

It only answers the question about IE browser version and ns4 in regard to broswer versions. I’d like to detect an older version of safari (as apposed to newer ones) but I doubt this plugin would accommodate that.

       
Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

Hi Rob Q,

I do not find any reason why Browser Sniff plugin should not accomodate older versions of Safari browser. You can try this code:

{exp:browser_sniff}
{if browser_name=="saf" AND browser_version<3}
Some code
{/if}
{/exp:browser_sniff}

Hope this helps.

       
Rob Quigley's avatar
Rob Quigley
236 posts
17 years ago
Rob Quigley's avatar Rob Quigley

Thanks for the reply, I’ll check it out and let you know how it goes!

       
1 2 3 Last

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.