Browser version detection: How?

R

R. Smits

I've have got this script, the only thing I want to be changed is the first
part. It has to detect IE version 6 instead of just "Microsoft Internet
Explorer". Can somebody help me out?
I tried "Microsoft Internet Explorer 6" but that doesn't work.

<SCRIPT LANGUAGE="Javascript">
<!--
bName = navigator.appName;
if (bName =="Microsoft Internet Explorer") {
document.write('<link media="screen" rel="STYLESHEET" type="text/css"
href="stylesheet1.css">')
} else {
document.write('<link media="screen" rel="STYLESHEET" type="text/css"
href="stylesheet2.css">')
}
//-->
</SCRIPT>
 
M

Michael Winter

I've have got this script, the only thing I want to be changed is the
first part. It has to detect IE version 6 instead of just "Microsoft
Internet Explorer". Can somebody help me out?
I tried "Microsoft Internet Explorer 6" but that doesn't work.

Scripting browser detection is highly unreliable, as browsers can, and do,
spoof IE. It would be more reliable to use conditional comments[1].

<link rel="stylesheet" type="text/css" href="stylesheet2.css"
media="screen">

<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="stylesheet1.css"
media="screen">
<[endif]-->

This would load stylesheet2 in all browsers, and stylesheet1 in only IE6.
The rules in stylesheet1 would override conflicting rules in stylesheet2.
<SCRIPT LANGUAGE="Javascript">

The language attribute has been deprecated in favour of the (required)
type attribute for quite some time now. Don't use it.


The practice of hiding scripts is similarly out-dated. There are no user
agents now in use that will render the contents of the script. All
browsers understand what a SCRIPT element is, even if they will just
ignore it (they don't support scripting).
bName = navigator.appName;

You might want to read the FAQ (<URL:http://jibbering.com/faq/>). Browser
detection, and what you should do instead, is covered there.

[snip]

Hope that helps,
Mike


[1]
<URL:http://msdn.microsoft.com/workshop/author/dhtml/overview/ccomment_ovw.asp>

Only IE 5 and later support conditional comments.
 
T

Thomas Hoheneder

I've have got this script, the only thing I want to be changed is the
first
part. It has to detect IE version 6 instead of just "Microsoft Internet
Explorer". Can somebody help me out?

The question is - do you really need exactly IE 6.x or would it be enough to
get some IE 4.x or higher version?
Consider, that if only asking for IE 6.x an IE 5.x e. g. would get
stylesheet2.css.
So I think you should use one stylesheet file for all IE 4.x or higher
version and some other stylesheet for all none-IE browsers (assuming that
there is no IE older than 4.x in use today).
Or do you really have such special stylesheet features that are only
supported by IE 6.x?

Nice greetings from
Thomas
 
R

R. Smits

Thomas Hoheneder said:
The question is - do you really need exactly IE 6.x or would it be enough to
get some IE 4.x or higher version?
Consider, that if only asking for IE 6.x an IE 5.x e. g. would get
stylesheet2.css.
So I think you should use one stylesheet file for all IE 4.x or higher
version and some other stylesheet for all none-IE browsers (assuming that
there is no IE older than 4.x in use today).
Or do you really have such special stylesheet features that are only
supported by IE 6.x?

Yep, I defined a 'overflow-y'.
As far as I know IE 6 is the only browers which supports this.
(Am I correct?)
 
R

R. Smits

Michael Winter said:
I've have got this script, the only thing I want to be changed is the
first part. It has to detect IE version 6 instead of just "Microsoft
Internet Explorer". Can somebody help me out?
I tried "Microsoft Internet Explorer 6" but that doesn't work.

Scripting browser detection is highly unreliable, as browsers can, and do,
spoof IE. It would be more reliable to use conditional comments[1].

<link rel="stylesheet" type="text/css" href="stylesheet2.css"
media="screen">

<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="stylesheet1.css"
media="screen">
<[endif]-->

This would load stylesheet2 in all browsers, and stylesheet1 in only IE6.
The rules in stylesheet1 would override conflicting rules in stylesheet2.

I tried something like this inside one stylesheet, but it didn't work.
Something like this:

..scroll {
height: 450px;
display:block;
overflow: scroll;
overflow-y: scroll;
}
<SCRIPT LANGUAGE="Javascript">

The language attribute has been deprecated in favour of the (required)
type attribute for quite some time now. Don't use it.


The practice of hiding scripts is similarly out-dated. There are no user
agents now in use that will render the contents of the script. All
browsers understand what a SCRIPT element is, even if they will just
ignore it (they don't support scripting).
bName = navigator.appName;

You might want to read the FAQ (<URL:http://jibbering.com/faq/>). Browser
detection, and what you should do instead, is covered there.

[snip]

Hope that helps,
Mike


[1]
<URL:http://msdn.microsoft.com/workshop/author/dhtml/overview/ccomment_ovw.a
sp>

Only IE 5 and later support conditional comments.

Thanks for your help.
 
T

Thomas Hoheneder

Yep, I defined a 'overflow-y'.
As far as I know IE 6 is the only browers which supports this.
(Am I correct?)

I am not sure, but I think you might be right. I tried it on IE 6 and it
worked, and on Mozilla Firefox it did not work.
So I think you want to have a query to detect IE 6.x on one side and all
other browsers on the other side. For the moment (where is no IE 7.x) I
would do this in the following way:

var strBVersion = new String(navigator.appVersion);
var bVersion = str.indexOf("MSIE 6");
if (bName == "Microsoft Internet Explorer" && bVersion != -1)
// IE 6.x
else
// some IE older than IE 6 or none IE browsers

But it could be (which is very presumably) an IE 7.x or something like that.
To support also this one, you should better modify the above instructions in
the following way to support all IE 6.x or higher versions - so a more
robust query would be:

var strBVersion = new String(navigator.appVersion);
var bVersion1 = str.indexOf("MSIE 6");
var bVersion2 = str.indexOf("MSIE 7");
if (bName == "Microsoft Internet Explorer" && bVersion1 != -1 && (bVersion1
!= -1 || bVersion2 != -1))
// IE 6.x or higher
else
// some IE older than IE 6 or none IE browser

Hope this helps you.

Nice greetings from
Thomas
 
T

Thomas Hoheneder

var bVersion = str.indexOf("MSIE 6");

I'm sorry, but "str.indexOf" must be replaced with "strBVersion.indexOf" in
both code variations.
 
R

Randy Webb

Thomas said:
I am not sure, but I think you might be right. I tried it on IE 6 and it
worked, and on Mozilla Firefox it did not work.
So I think you want to have a query to detect IE 6.x on one side and all
other browsers on the other side.

No, you don't have to worry about the browser. If you use a CSS
definition that the browser doesn't understand, it ignores it. Meaning,
when any browser that encounters div { myOffTheWallAttribute: 0px;} it
will simply ignore it.

For the moment (where is no IE 7.x) I would do this in the following way:

And you would fail.
var strBVersion = new String(navigator.appVersion);
var bVersion = str.indexOf("MSIE 6");
if (bName == "Microsoft Internet Explorer" && bVersion != -1)
// IE 6.x
else
// some IE older than IE 6 or none IE browsers

You corrected the str. mistake, but where is bName defined?

This is the navigator.appVersion as reported by AOL 9.0, which passes
your test but it is *not* IE6.0

4.0 (compatible; MSIE 6.0; AOL 9.0; Windows NT 5.1; SV1)

I think what you were after was the appName, but this is what AOL reports:

Microsoft Internet Explorer

Again, its not Internet Explorer, but a shell of it. In any event, its
irrelevant to the question. That is the same appName as given by Opera
7 in IE spoof mode.
But it could be (which is very presumably) an IE 7.x or something like that.
To support also this one, you should better modify the above instructions in
the following way to support all IE 6.x or higher versions - so a more
robust query would be:

var strBVersion = new String(navigator.appVersion);
var bVersion1 = str.indexOf("MSIE 6");
var bVersion2 = str.indexOf("MSIE 7");
if (bName == "Microsoft Internet Explorer" && bVersion1 != -1 && (bVersion1
!= -1 || bVersion2 != -1))
// IE 6.x or higher
else
// some IE older than IE 6 or none IE browser

And it still won't work. See above.
 
R

Richard Cornford

IE 5.0 supports overflow-x/y (and IE 4 may also).

... I would do this in the following way:

var strBVersion = new String(navigator.appVersion);
var bVersion = str.indexOf("MSIE 6");
if (bName == "Microsoft Internet Explorer" && bVersion != -1)
// IE 6.x
else
// some IE older than IE 6 or none IE browsers

But it could be (which is very presumably) an IE 7.x or
something like that. To support also this one, you should
better modify the above instructions in the following way
to support all IE 6.x or higher versions - so a more robust
query would be:

It has not been Microsoft's style to remove anything form IE once they
have introduced it, so overflow-x/y are likely to remain part of their
browser in future versions.
var strBVersion = new String(navigator.appVersion);
var bVersion1 = str.indexOf("MSIE 6");
var bVersion2 = str.indexOf("MSIE 7");

It might be another 2 years before we see the next IE version, but after
that they may produce a number of versions in rapid succession. So they
may be on IE 8 within a year of IE 7 (assuming they decide to use
sequential version numbers instead of some naming or coding system, as
appears to be recent fashion).

One of the more obvious flaws in browser detection is the anticipation
of future versions of browsers. The assumption that the next version of
IE will be identifiable as "MSIE 7", even if true, will still mean that
this code will need maintenance when the following version comes out.
if (bName == "Microsoft Internet Explorer" && bVersion1 != -1 &&
(bVersion1 != -1 || bVersion2 != -1))
// IE 6.x or higher
else
// some IE older than IE 6 or none IE browser

Hope this helps you.

It never helps to be recommending browser detection in any form, but
particularly based on properties of the - navigator - object. The
assumed relationships required for this style of detection are not even
true for current browsers. This particular 'test' will have
misidentified IceBrowser and NetFront as IE 6 by default, and may also
end up treating half a dozen other browsers as IE 6 depending on user
preference settings. It is really unrelated to the problem it is trying
to solve and as a result will be wide of the mark much of the time.

Feature detection testing is based around trying to determine support
for any particular feature of web browsers in a way that is closely
related to the problem (preferably by a one-to-one relationship). In
this case the question of interest is; does the browser support the
overflow-x/y style properties (and has nothing to do with whether that
browser actually is an IE version or not). That question might best be
answered by examining a - style - object, be it the style object
belonging to some DOM element, or retrieved from a style rule in the -
document.styleSheets - object. A browser that supports overflow-x/y - is
likely to manifest - overflowX/Y - properties on its - style - objects,
while a browser lacking support almost certainly will not. So, given a
reference to a - style - object, a discriminating feature detection test
that has a clear direct relationship with the browser's support for the
feature of interest would be:-

if( typeof aStyleObject.overflowY == 'string'){
... // browser can be expected to support overflow-y.
}

And like feature detection testing in general, not only does it answer
the question that needs to be answered (rather than some other question
that is assumed to be related to the first in some (often invalid) way),
it is also future proof as it no longer matters whether the next (or
subsequent) version of IE supports the feature (or indeed whether the
browser is a Microsoft browser at all). So instead of writing code that
you know will require future maintenance (when IE 8 is released) you are
writing code that stands a good chance of never needing looking at again
(once it is properly tested).

Of course CSS problems are best tacked with CSS techniques (as Randy
mentioned) rather than scripting.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,210
Latest member
BestCrypto Consultant

Latest Threads

Top