LinuxSA Mailing list archives
Index:
[thread]
[date]
[subject]
[author]
[stats]
From: Mike Gratton <mike@vee.net>
To : <andrewlord@internode.on.net>
Date: Tue, 29 Apr 2003 02:07:44 +0930
Re: navigator.appName
Andrew Lord wrote:
>
> Thanks for your answer.
>
NP!
>
> Excellent. My JS skills ain't much, but they're improving (painfully) slowly.
> I'll give your suggestion a go.
>
If you're trying to do cross-browser scripting, then you should be
looking to do, in order from worst to best:
1) UA string scanning to determine browser and version. This sucks.
2) DOM object detection to determine browser and version. This sucks
slightly less.
3) DOM object detection to determine what methods and properties you
have available to use. This is the best way.
DOM object detection works by examining the objects you're using to see
if it supports the required properties and methods.
For exmple, if you had an HTML document:
<html><body><p id="foo">This is a paragraph</p></body></html>
and you wanted to retrieve a reference to the "p" element, you would do
something like:
var foo;
if (document.getElementById) {
// gecko, IE6 (in standards mode), etc
foo = document.getElementById("foo");
} else if (document.all) {
// IE6 (in compatibility mode), IE5.x, IE4.x
foo = document.all["foo"];
} else if (document.layers) {
// NN 4
foo = document.layers["foo"];
} else {
foo = null;
alert("Could not get foo!");
}
Of course, you would probably turn this into a function so you can
easily reuse it.
The reason DOM object detection is better than UA sniffing is becuase it
will always work, for past, present and future browsers and it is never
wrong. You don't need to constantly update your scripts when a new
version of a browser comes out. Using nifty Javascript hacks it is
possible to provide implementations of the standard DOM methods when
missing, so even on crusty browsers like IE4/5, it is becomes possible
to use the DOM in an effective way.
HTH,
/mike
--
Mike Gratton <mike@vee.net>
I want you to know, he's not coming back.
Blatant self-promotion: <http://web.vee.net/>
--
LinuxSA WWW: http://www.linuxsa.org.au/ IRC: #linuxsa on irc.freenode.net
To unsubscribe from the LinuxSA list:
mail linuxsa-request@linuxsa.org.au with "unsubscribe" as the subject
Index:
[thread]
[date]
[subject]
[author]
[stats]
Return to the LinuxSA Mailing List Information Page