1 of 3
1
Plugin: Browser Sniff
Posted: 30 March 2008 03:25 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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.

File Attachments
pi.browser_sniff_v1.2.zip  (File Size: 3KB - Downloads: 108)
 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 05 April 2008 03:17 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  53
Joined  08-09-2007

Nice work! I like this plugin.

Examples:
Warning for old IE6

{exp:browser_sniff}
{if browser_name==“ie” AND browser_version<=6}
        <b>ATTENTION! This site is optimized for the actual IE, please update your browser.<br /> <a href=“http://www.microsoft.com/windows/downloads/ie/getitnow.mspx”>Microsoft Update Website/images/system/browser-ie.gif</a></b>
{/if}
{/exp:browser_sniff}

Specific Bookmark

{exp:browser_sniff}
{if browser_name==“ie”}
<br />
{/if}
{/exp:browser_sniff}

{exp:browser_sniff}
{if browser_name==“moz” OR browser_name==“ns4”}

<br />
{/if}
{/exp:browser_sniff}

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

Profile
 
 
Posted: 05 April 2008 03:25 AM   [ Ignore ]   [ # 2 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6405
Joined  04-15-2006

Hi Ronin,

Should there be some code in where you have the <br />‘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 grin

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 05 April 2008 03:26 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  53
Joined  08-09-2007

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

Profile
 
 
Posted: 05 April 2008 03:29 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  53
Joined  08-09-2007

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="ja_vascr_ipt:wind_ow.exte_rnal.A_ddFavo_rite('url', 'title')">
<
img src="/images/system/site_tools_bookmark.gif" alt="add to favorites" title="add to favorites" /> <strong>add to favorites</strong></a><br />
{/if}
{
/exp:browser_sniff}

{exp
:browser_sniff}
{if browser_name
=="moz" OR browser_name=="ns4"}
<a href="ja_vascr_ipt:win_dow.sid_ebar.a_ddPa_nel('title', 'url', '');">
<
img src="/images/system/site_tools_bookmark.gif" alt="add to bookmarks" title="add to bookmarks" /> <strong>add to bookmarks</strong></a>
<
br />
{/if}
{
/exp:browser_sniff}

Profile
 
 
Posted: 05 April 2008 03:29 AM   [ Ignore ]   [ # 5 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6405
Joined  04-15-2006

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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 05 April 2008 03:40 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  53
Joined  08-09-2007

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

Profile
 
 
Posted: 05 April 2008 03:55 AM   [ Ignore ]   [ # 7 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6405
Joined  04-15-2006

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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 06 April 2008 08:36 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 09 May 2008 03:39 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Avatar
Rank
Total Posts:  68
Joined  03-05-2008

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

Profile
 
 
Posted: 18 July 2008 09:40 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

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

{if browser_name=="ie" AND browser_version<=6}

Profile
 
 
Posted: 20 July 2008 12:14 PM   [ Ignore ]   [ # 11 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6405
Joined  04-15-2006

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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 20 July 2008 01:55 PM   [ Ignore ]   [ # 12 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

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.

Profile
 
 
Posted: 21 July 2008 01:27 AM   [ Ignore ]   [ # 13 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 21 July 2008 08:41 AM   [ Ignore ]   [ # 14 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

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

Profile
 
 
Posted: 21 July 2008 10:37 AM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

I gave this a try using this code

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

and it didn’t work for me. I used a lower level safari browser 1.3 and 2 and neither was detected as being a lesser version of 3 (or 3.1). In a separate test it detected both browsers as being safari though.

Profile
 
 
Posted: 21 July 2008 11:03 AM   [ Ignore ]   [ # 16 ]  
Grad Student
Avatar
Rank
Total Posts:  68
Joined  03-05-2008

Did you check what you get with the following?

{exp:browser_sniff}
<p>
browser name: {browser_name}<br />
browser version: {browser_version}
</p>
{/exp:browser_sniff}


Also you might want to try what $_SERVER[“HTTP_USER_AGENT”] in general is showing as a difference between the browser versions.
Here’s a test page for this:
http://sqa.fyicenter.com/Online_Test_Tools/__Show_Web_Browser_Information.php

Profile
 
 
Posted: 21 July 2008 11:53 AM   [ Ignore ]   [ # 17 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

Thanks for the suggestions.

I’m not receiving any info back in the template using these plugin tags below with any of the safari browsers:

{exp:browser_sniff}
<p>
browser name: {browser_name}<br />
browser version: {browser_version}
</p>
{/exp:browser_sniff}

I went to that browser detection page with Safari 3.1 browser and a 1.3.2 browser. I took screen shots of the version info detected and attached a combined image of both below (one on top is the 3.1 version).

Basically, it looks like it detects a version number for the 3.1 browser but not for the earlier ones. It does provide other numbers for the browsers (525.13 & 312.6) but I wasn’t having any luck trying to use the conditional with those.

Image Attachments
safari-browswers.jpg
Click thumbnail to see full-size image
Profile
 
 
Posted: 21 July 2008 02:30 PM   [ Ignore ]   [ # 18 ]  
Grad Student
Avatar
Rank
Total Posts:  68
Joined  03-05-2008

I had a look at the code at how the needed functionality can be added.

So I added three lines of code to accomplish what we want here. Now you get return values for {browser_name} and {browser_version}. This way you can check what gets detected. I’ve attached a new version to this post and I’ve updated the plugin documentation.

I’m no programmer and I don’t know much about PHP, I just browsed the PHP and EE developer docs for a while to figure out how I could return the conditionals evaluation and swap the template variables content. Looks like it turned out well. If someone feels I missed something please let me know.

The following should now work as expected:

{exp:browser_sniff}
<p>
browser name: {browser_name}<br />
browser version: {browser_version}
</p>
{/exp:browser_sniff}

Cheers,
Timon

File Attachments
pi.browser_sniff-v1.1.9.zip  (File Size: 3KB - Downloads: 50)
Profile
 
 
   
1 of 3
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64494 Total Logged-in Users: 53
Total Topics: 81044 Total Anonymous Users: 33
Total Replies: 436121 Total Guests: 270
Total Posts: 517165    
Members ( View Memberlist )