G
Garrett Smith
8.6 _How do I detect Opera/Netscape/IE_?
The navigator object contains strings which
specify the browser and version; however, this is in general not
very genuine. Mozilla (and therefore Netscape 6+) allows this to
be freely set, and Opera and IE allow it to be modified. There
are also at least 25 other javascript capable browsers with
their own strings here.
Generally though, you don't need to identify which browser is
being used. There are alternative techniques, but which one you
choose depends on why you want to redirect browsers. If it's to
offer different CSS stylesheets, then <URL:
http://w3development.de/css/hide_css_from_browsers/ >
shows many techniques. For Scripting, _object_ detection
is a better method to use.
<URL: http://www.quirksmode.org/js/support.html >
It is also known as feature detection.
Object/feature detection means checking that the object you wish
to use is supported by the browser before using it. This means
that you don't need to know what browsers support what methods,
and your code will automatically be usable on any browser that
can execute it.
if (document.getElementById &&
document.getElementById('el') &&
document.getElementById('el').style ) {
// We know that this browser supports getElementById and has
// a style object, so we can set a style property.
document.getElementById('el').style.color = "red";
}
Browser bugs can often be detected and overcome in similar ways.
<URL:
http://developer.mozilla.org/en/doc...Developing_Cross_Browser/Cross_Platform_Pages<URL: http://jibbering.com/faq/faq_notes/not_browser_detect.html >
<URL: http://dev.opera.com/articles/view/using-capability-detection/ >
<URL:
http://developer.apple.com/internet/webcontent/objectdetection.html >
==========================================
Commentary:
The entry links to the quirksmode.org article:
http://www.quirksmode.org/js/support.html
While well intentioned, this article has some information that is
factually false and would likely mislead someone learning javascript. I
found in that article:
| if (window.focus)
| means: "If the focus method is supported", while this code
| if (window.focus())
| means:
| "If you can put the focus on the window" and assumes that focus
| is supported.
Both statements are wrong.
Regarding the first statement, I am concerned with the possibility of a
browser to be configured to disallow scripts to focus windows, yet have
a focus property.
Regarding the second statement, that is just dead wrong. The
window.focus method call should return undefined where supported and the
|if| statement would be false.
He also calls document.images and "array". It is not. It is an
HTMLCollection in standards-compliant browsers.
I have added a link to Hallvord R.M. Steen's "Object Detection". The
article does not mention ToBoolean errors on ActiveX objects. I sent an
email to Hallvord and asked him to add something about that.
Rearding the the FAQ, it is not to the point enough.
| Generally though, you don't need to identify which browser is being
| used. There are alternative techniques, but which one you choose
| depends on why you want to redirect browsers.
Not to the point. Redirect browsers? No, that is not usually why browser
detection is used. "Alternative" techniques? I'd rather think of
capability detection as a "mroe sensible technique"
I think the whole thing needs a rewrite.
The points should be that browser detection is:
1) unreliable (can't trust navigator.userAgent)
2) uses an unrelated inference (it is unrelated to the problem it is
trying to solve), which:
a. causes forwards-compatibility problems
b. hides the reasoning behind the workaround, which makes
understanding the thinking behind the code more difficult to understand.
Though I haven't drafted anything. Still working on the notes index page.
Garrett
The navigator object contains strings which
specify the browser and version; however, this is in general not
very genuine. Mozilla (and therefore Netscape 6+) allows this to
be freely set, and Opera and IE allow it to be modified. There
are also at least 25 other javascript capable browsers with
their own strings here.
Generally though, you don't need to identify which browser is
being used. There are alternative techniques, but which one you
choose depends on why you want to redirect browsers. If it's to
offer different CSS stylesheets, then <URL:
http://w3development.de/css/hide_css_from_browsers/ >
shows many techniques. For Scripting, _object_ detection
is a better method to use.
<URL: http://www.quirksmode.org/js/support.html >
It is also known as feature detection.
Object/feature detection means checking that the object you wish
to use is supported by the browser before using it. This means
that you don't need to know what browsers support what methods,
and your code will automatically be usable on any browser that
can execute it.
if (document.getElementById &&
document.getElementById('el') &&
document.getElementById('el').style ) {
// We know that this browser supports getElementById and has
// a style object, so we can set a style property.
document.getElementById('el').style.color = "red";
}
Browser bugs can often be detected and overcome in similar ways.
<URL:
http://developer.mozilla.org/en/doc...Developing_Cross_Browser/Cross_Platform_Pages<URL: http://jibbering.com/faq/faq_notes/not_browser_detect.html >
<URL: http://dev.opera.com/articles/view/using-capability-detection/ >
<URL:
http://developer.apple.com/internet/webcontent/objectdetection.html >
==========================================
Commentary:
The entry links to the quirksmode.org article:
http://www.quirksmode.org/js/support.html
While well intentioned, this article has some information that is
factually false and would likely mislead someone learning javascript. I
found in that article:
| if (window.focus)
| means: "If the focus method is supported", while this code
| if (window.focus())
| means:
| "If you can put the focus on the window" and assumes that focus
| is supported.
Both statements are wrong.
Regarding the first statement, I am concerned with the possibility of a
browser to be configured to disallow scripts to focus windows, yet have
a focus property.
Regarding the second statement, that is just dead wrong. The
window.focus method call should return undefined where supported and the
|if| statement would be false.
He also calls document.images and "array". It is not. It is an
HTMLCollection in standards-compliant browsers.
I have added a link to Hallvord R.M. Steen's "Object Detection". The
article does not mention ToBoolean errors on ActiveX objects. I sent an
email to Hallvord and asked him to add something about that.
Rearding the the FAQ, it is not to the point enough.
| Generally though, you don't need to identify which browser is being
| used. There are alternative techniques, but which one you choose
| depends on why you want to redirect browsers.
Not to the point. Redirect browsers? No, that is not usually why browser
detection is used. "Alternative" techniques? I'd rather think of
capability detection as a "mroe sensible technique"
I think the whole thing needs a rewrite.
The points should be that browser detection is:
1) unreliable (can't trust navigator.userAgent)
2) uses an unrelated inference (it is unrelated to the problem it is
trying to solve), which:
a. causes forwards-compatibility problems
b. hides the reasoning behind the workaround, which makes
understanding the thinking behind the code more difficult to understand.
Though I haven't drafted anything. Still working on the notes index page.
Garrett