| Description: |
During compatibilty testing of a new site I'm working on, I needed a way to detect browsers and act accordingly. This short and sweet script does just that, and removes redundancy. (I'm also teaching myself hash and array references)
|
my @user_agent_list=
({title=>'Internet Explorer',
string=>'MSIE',
url=>'http://www.microsoft.com/windows/ie/defa
+ult.asp'},
{title=>'Konqueror',
string=>'Konqueror',
url=>'http://www.konqueror.org/'},
{title=>'Galeon',
string=>'Galeon',
url=>'http://galeon.sourceforge.net/'},
{title=>'Opera',
string=>'Opera',
url=>'http://www.opera.com/'},
{title=>'Dillo',
string=>'Dillo',
url=>'http://dillo.cipsga.org.br/'},
{title=>'Mozilla',
string=>'Mozilla\/5',
url=>'http://www.mozilla.org/'},
{title=>'Netscape',
string=>'Mozilla\/4',
url=>'http://wp.netscape.com/download/'},
);
print div({-id=>'browser'}, p("Browser: $ENV{'HTTP_USER_AGENT'}"),);
for my $browser (@user_agent_list) {
if (($ENV{'HTTP_USER_AGENT'}) =~ /$browser->{string}/) {
print p("You seem to be using",
a({-href=>"$browser->{url}"},
$browser->{title}, ));
}
}
Update
I just found a slight flaw in this design. Microsoft Internet Explorer on Win32 platforms displays a $ua->agent string of:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
..and that also matches what Netscape displays. Running this against those two browsers will print out two lines for each of them (IE matches Netscape and IE, and vice-versa for Netscape).
I'll try to find a way around this without having to use !~ matching. |
Re: Short and Sweet Browser Detection by KM (Priest) on Jun 07, 2002 at 17:27 UTC |
I would suggest making your structure a hash, so you don't have to loop. With a long list (like if you add Lynx, Chimera, iCab, DoCoMo, etc...) the looping is uneeded.
my $user_agent = {"Mozilla" => {string => "Mozilla", url => "http://mo
+zilla.org"},
"Opena" => # etc...
};
May serve you well. Just make sure the keys are the user agent info you want, and manipulate $ENV{USER_AGENT} to remove any OS info (assuming you don't want that).
Cheers,
KM | [reply] [d/l] |
|
But... You'll still need to loop through, trying a match with each until you find the user agent.
Perhaps looking for the element in user_agent that contains a slash, then stripping the version number, and using that as an index into a hash. i.e. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" becomes "Mozilla/4.0", then "Mozilla" which is used as a key in the hash.
But then that may be a bit of overkill unless you're planning on matching every possible browser/wap device. Even then, the list would be under a couple hundred.
| [reply] [d/l] [select] |
|
Just have the keys be what the USER_AGENT string will be. No looping, that's what exists() is for.
Cheers,
KM
| [reply] |
|
|
|
Re: Short and Sweet Browser Detection by chromosundrift (Novice) on Jun 16, 2002 at 11:56 UTC |
You really should AVOID even looking at user agent strings. It is possible to write cross browser html and javascript without checking for particular browsers. Failing to do this always makes the site more brittle and less maintainable. | [reply] |
|
| [reply] |
Back to Snippets Section
|
|