Browser sniffer as server-side include
The advantage of making a browser sniffer as server-side include is that it can be contained entirely in your HTML file.
Example 1:
<!--#if expr="$HTTP_USER_AGENT = /MSIE/" --> <!-- browser is Explorer --> <MARQUEE>Important message</MARQUEE> <!--#elif expr="$HTTP_USER_AGENT = /Mozilla/ && $HTTP_USER_AGENT != /compatible/ && $HTTP_USER_AGENT != /Opera/ && $HTTP_USER_AGENT != /Spoofer/" --> <!-- browser is Netscape --> <BLINK>Important message</BLINK> <!--#else --> <!-- browser is not Explorer or Netscape --> <FONT COLOR='#FF0000' SIZE=+1>Important message</FONT> <!--#endif --> |
Explanation:
This code is intended to display the text 'Important message' in a way that attracts attention. In Netscape it produces the HTML tag <BLINK> which is only supported in Netscape. In Explorer it produces the HTML tag <MARQUEE> which is only supported in Explorer. In other browsers it writes the text in red.
The slashes (/ /) in the <!--#if .. --> directive mean 'search for matching string'. If the HTTP_USER_AGENT string contains the word MSIE, then it is Explorer. If it contains the word Mozilla, and none of the words compatible, Opera, or Spoofer, then it is Netscape.
The result is shown here:
| Important message |
Example 2:
If we want to check not only the brand of browser, but also the version number, then the code becomes a rather awkward, so you may want to use PHP instead. There is no server-side directive that can extract the version numbers, so if you want to do it with SSI then you have to rely on pattern-matching. The syntax for pattern matching is the same as for the UNIX egrep command. The following example checks the browser type and version number in order to determine which version of the Document Object Model (DOM) it supports.
<!--#if expr="$HTTP_USER_AGENT = /MSIE [4-5]/"
&& $HTTP_USER_AGENT = /Windows/ -->
Browser is Explorer version 4 or 5. Use MS-DOM.
<!--#elif expr="$HTTP_USER_AGENT = /Mozilla.4\./
&& $HTTP_USER_AGENT != /Opera/
&& $HTTP_USER_AGENT != /compatible/" -->
Browser is Netscape version 4. Use Netscape-DOM.
<!--#elif expr="$HTTP_USER_AGENT = /Mozilla.\([5-9]|[0-9]\{2,\}\)/
&& $HTTP_USER_AGENT != /Opera/
&& $HTTP_USER_AGENT != /compatible/" -->
Browser is Netscape version 5 or later. Use W3C-DOM.
<!--#else -->
Other browser. Don't use DOM.
<!--#endif -->
|
The result is shown here:
| Other browser. Don't use DOM. |
Note: It is not known whether future versions of Explorer will use MS-DOM or W3C-DOM.
This page was last modified 2008-Dec-08
