Back to Programming Articles

Restrict code in the templates to certain browsers.
by Brad 09 Feb 2006

This thread will teach you how to restrict code in the templates to certain browsers.

Before we begin

This method is not a substitute for proper html and css. Also the way we determine the browser the client is using can easily be spoofed, it's not hard to make vBulletin think firefox is really IE for example.

In other words this method should only be used when there is no other way to get the job done.

The good stuff

There is a function within the vBulletin source code call is_browser. With this function we can determine what type of browser the client is using, and which version of the browser the client is using.

In any given template, you can use this code to display a short message to clients that use Internet Explorer:

HTML Code:
<if condition="is_browser('ie')"><b>You are using Internet Explorer</b></if>
We can also display a message based on the version number of Internet Explorer:

HTML Code:
<if condition="is_browser('ie', 6)"><b>You are using Internet Explorer version 6</b></if>
<if condition="is_browser('ie', 5)"><b>You are using Internet Explorer version 5</b></if>
So why is this useful? Well lets say for some odd reason you do not support Internet Explorer 5 or lower on your site and you would like to display a short message to users visiting your site with that browser. Well we can do something like this:

HTML Code:
<if condition="!is_browser('ie', 6)"><b>mysite.com only supports Internet Explorer 6.0 and higher, please upgrade your browser</b></if>
This conditional would display "mysite.com only supports Internet Explorer 6.0 and higher, please upgrade your browser" in bold to any client running Internet Explorer version 5.5 or lower. Also before anyone brings this up, no it will not display the message to clients using Internet Explorer version 7. In other words the function is coded in such a way that your code is future proof.

Here is a list of browsers supported by the is_browser function. The call sign is on the left (the name you should use in the function call), the browser name that call sign relates to is on the right.

Supported browsers

opera => http://www.opera.com/
ie => http://www.microsoft.com
mozilla => http://www.mozilla.org/products/mozilla1.x/ and Mozilla based browsers.
firebird => Older versions of Firefox (below)
firefox => http://www.mozilla.com/firefox/
camino => http://www.mozilla.org/products/camino/
konqueror => http://www.konqueror.org/
safari => http://www.apple.com/safari/download/
webkit => http://webkit.opendarwin.org/ based applications
webtv => http://developer.msntv.com/Tools/WebTVVwr.asp
netscape => http://browser.netscape.com/

Similar Mods

Restrict [code] and [php] to registered members only vBulletin 2.x Full Releases

vblts.ru supports vBulletin®, 2022-2024