![]() |
Operator to detect user's platform/browser
Misha [October 18, 2005]
Sometimes, we may need to carry out different actions for different browsers. In this case, we should first detect user's browser/platform.
The only simple way to do it is to analyze environment variable $env:HTTP_USER_AGENT and then carry out actions depending on the result.
Class detect have two static methods (os and agent), which analyze specified string with HTTP_USER_AGENT value and get back hashes with browser or platform information.
Example of usage detect class:
@main[] ^use[Detect.p] $hPlatform[^Detect:os[$env:HTTP_USER_AGENT]] $hBrowser[^Detect:agent[$env:HTTP_USER_AGENT]] OS: $hPlatform.name<br /> Browser: $hBrowser.name ${hBrowser.ver}.${hBrowser.subver}Download:
Detect.zip (29.03.2007 570 bytes)
Class DetectVery old operator detectBrowser, which do the same thing. During call this operator store browser and platform info in global variables $MAIN:browser, $MAIN:browser_ver, $MAIN:browser_subver and $MAIN:platform.
Example of usage detectBrowser operator:
@main[] ^use[implode.p] ^detectBrowser[] OS: $MAIN:platform<br /> Browser: $MAIN:browser ${MAIN:browser_ver}.${MAIN:browser_subver}Download:
implode.zip (03.12.2002 480 bytes)
Operator detectBrowserIt should be noted that these methods aren't universal, but they can be just enough in simple cases. The best way is to generate the same html for all browsers and make browser specific things with JavaScript. We should also remember that when a visitor is on proxy server, this environment variable cannot be relied upon.
| E-mail: mailbox@parser.ru | Copyright © 1997–2010 | Art Lebedev Studio |