Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Browser Check

Reply
Thread Tools

Browser Check

 
 
McKirahan
Guest
Posts: n/a
 
      02-25-2004
Is this a very good browser check?

<html>
<head>
<title>wB.htm</title>
<script type="text/javascript">
var adBtype = "??";
function wB() {
if (document.getElementById && !document.all) {
adBtype = "NS6+";
} else if (document.getElementById && document.all) {
adBtype = "IE5+";
} else if (document.all) {
adBtype = "IE4";
} else if (document.layers) {
adBtype = "NS4";
}
alert(adBtype);
}
</script>
</head>
<body onload="wB()">
<body>
</html>

Opera 6.01 returns "IE5+"; don't know about others...


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      02-25-2004
"McKirahan" <(E-Mail Removed)> writes:

> Is this a very good browser check?


No. I can say that without reading, because browser checks are almost
invariably not good, no matter how efficient they are .

(Remember DOCTYPE, it is required by HTML)

> var adBtype = "??";
> function wB() {
> if (document.getElementById && !document.all) {
> adBtype = "NS6+";


Or Opera 6 or ....

> } else if (document.getElementById && document.all) {
> adBtype = "IE5+";


Or Opera 6 (in IE mode) or Opera 7 or ...

> } else if (document.all) {
> adBtype = "IE4";


Or WebTV or ....

> } else if (document.layers) {
> adBtype = "NS4";


Or OmniWeb or ...

> Opera 6.01 returns "IE5+"; don't know about others...


Not knowing about others is *the* problem with browser detection.
Optimistically it is hard work to become familiar with all browsers,
realistically, it is quite impossible. You will invariably miss some
browsers that are in actual use, and will most likely badly
misrepresent future browsers.

The way to make scripts usefull across as many browsers as possible
are:
1) Use standards! It gives you the best chance of forward compatability.
2) Use object/feature detection, not browser detection, to make fallbacks
for non-standard-compliant browsers.

Object detection coulde be:

var elem;
if (document.getElementById) {
elem = document.getElementById(id);
} else if (document.all) {
elem = document.all[id];
} else if (document.layers) {
elem = document.layers[id];
} else {
elem = null;
}

Before using a standard feature that some browsers are known to be
incompatible with, you test for its existence. If it doesn't exist,
you test for the existence of some other feature that might (or might
not) be used instead. Notice that the above code doesn't care whether
it is IE 5+ or NS 6+ or whatever. It is not perfect code! There are
cases where it fails too, which further testing could fix. And some
that it can't (some browsers are just broken, or doesn't have
Javascript enabled).

It is important to realize that old browsers that nobody uses will not
become more used in the future, but future browsers will. That makes
forward compatability more important than backwards compatability.
Detecting specific versions of known browsers will necessarily not
work for future versions.


/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Mick White
Guest
Posts: n/a
 
      02-26-2004
McKirahan wrote:


> <script type="text/javascript">
> var adBtype = "??";
> function wB() {
> if (document.getElementById && !document.all) {
> adBtype = "NS6+";
> } else if (document.getElementById && document.all) {
> adBtype = "IE5+";
> } else if (document.all) {
> adBtype = "IE4";
> } else if (document.layers) {
> adBtype = "NS4";
> }
> alert(adBtype);
> }
></script>


There's no escape for the function:


function wB() {
if (document.getElementById && !document.all) {
adBtype = "NS6+";return;
}
if (document.getElementById && document.all) {
adBtype = "IE5+";return;
}
if (document.all) {
adBtype = "IE4";return;
}
if (document.layers) {
adBtype = "NS4";return;
}
adBtype= "unknown to this programmer";
}
Mick
 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      02-26-2004
"Mick White" <(E-Mail Removed)> wrote in message
news:0Fb%b.92008$%(E-Mail Removed)...
> McKirahan wrote:
>
>
> > <script type="text/javascript">
> > var adBtype = "??";
> > function wB() {
> > if (document.getElementById && !document.all) {
> > adBtype = "NS6+";
> > } else if (document.getElementById && document.all) {
> > adBtype = "IE5+";
> > } else if (document.all) {
> > adBtype = "IE4";
> > } else if (document.layers) {
> > adBtype = "NS4";
> > }
> > alert(adBtype);
> > }
> ></script>

>
> There's no escape for the function:
>
>
> function wB() {
> if (document.getElementById && !document.all) {
> adBtype = "NS6+";return;
> }
> if (document.getElementById && document.all) {
> adBtype = "IE5+";return;
> }
> if (document.all) {
> adBtype = "IE4";return;
> }
> if (document.layers) {
> adBtype = "NS4";return;
> }
> adBtype= "unknown to this programmer";
> }
> Mick


Actually I modified one I found; it actually was:

function wB() {
if (document.getElementById && !document.all) {
adBtype = "NS6+";
return;
} else if (document.getElementById && document.all) {
adBtype = "IE5+";
return;
} else if (document.all) {
adBtype = "IE4";
return;
} else if (document.layers) {
adBtype = "NS4";
return;
}
}


 
Reply With Quote
 
kaeli
Guest
Posts: n/a
 
      02-26-2004
In article <Jm9%b.56686$Xp.269424@attbi_s54>, (E-Mail Removed)
enlightened us with...
> Is this a very good browser check?


No, but it's a great way of doing object detection.

There is NO good browser check. There are simply too many browsers.

--
--
~kaeli~
I do whatever my Rice Krispies tell me to.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      02-26-2004
McKirahan wrote:


>
> Actually I modified one I found; it actually was:
>
> function wB() {
> if (document.getElementById && !document.all) {
> adBtype = "NS6+";
> return;
> } else if (document.getElementById && document.all) {
> adBtype = "IE5+";
> return;
> } else if (document.all) {
> adBtype = "IE4";
> return;
> } else if (document.layers) {
> adBtype = "NS4";
> return;
> }
> }
>


What if all the Booleans are false? You need to declare a global variable:
var adBtype="Unknown" // A suggestion.
But read the other comments, there are probably hundreds of different
browsers out there, some of which obfuscate their headers. IOW your
"adBtype" is just a guess.
Mick
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to selecet check box in the data grid ?? only one check box mit ASP .Net 1 01-25-2006 06:47 PM
how to check all check boxes in repeater control kris ASP .Net 1 09-22-2005 07:16 PM
how to check all check boxes in repeater control kris ASP .Net 0 09-22-2005 06:29 PM
how to check all check boxes in repeater control kris ASP .Net 0 09-22-2005 06:29 PM
.Net Pro include Visual Source Safe or other Code Check in Check out software? Davisro ASP .Net 1 06-14-2004 03:23 PM



Advertisments