Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting Browser suggestion

37 views
Skip to first unread message

Tim Romano

unread,
Jan 1, 2003, 9:53:33 AM1/1/03
to
[cross-posted to wishlist, page-authoring, and promote.]

If many sites are not properly detecting the (latest) Opera browser
version, perhaps Opera could write and release into the public domain
some elegant and well-commented browser-detection script that does things
correctly vis-a-vis Opera? The code could be placed on a Detecting Opera
page at www.opera.com. It would be good if the code were always up-to-
date.

Maybe this elegant Opera-blessed browser detection code already exists
and is prominently displayed on Opera's site somewhere? Searching the
site halfheartedly did not discover it.

Opera could mention the Detecting Opera link in the Readme file and the
online Help could have a hyperlink to the Detecting Opera page.

Thanks
Tim Romano


Jor

unread,
Jan 1, 2003, 10:42:25 AM1/1/03
to
On Wed, 1 Jan 2003 09:53:33 -0500, Tim Romano
<timUNDERS...@yahoo.com> wrote:

> [cross-posted to wishlist, page-authoring, and promote.]
>
> If many sites are not properly detecting the (latest) Opera browser
> version, perhaps Opera could write and release into the public domain
> some elegant and well-commented browser-detection script that does things
> correctly vis-a-vis Opera? The code could be placed on a Detecting Opera
> page at www.opera.com. It would be good if the code were always up-to-
> date.
>

This is really easy actually, IF you have to use if. If your code is simply
standards-compliant, there is absolutely no need for any sniffer script.
Browser detection is a last effort you can use to work around some browser
bugs (to serve NS4 a non-CSS version of a site for example), but this can
be better done with a proper stylesheet. It is perfectly possible to write
a stylesheet for a page which lets it degrade gracefully from top-of-the-
line browsers like Opera 7, to text-only browsers like Links and Lynx.

Because some people started browser-sniffing in 1997, many sites to this
day lock out Opera and other new browsers. If people would simply learn
HTML and ECMAscript ("Javascript"), this practice could end today, and
Opera and Mozilla could remove their fake UA strings.

> Maybe this elegant Opera-blessed browser detection code already exists
> and is prominently displayed on Opera's site somewhere? Searching the
> site halfheartedly did not discover it.
>

Here is a simple, but powerful script that detects many kinds of browsers,
but you really should not have to use it:

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--

var WIN = navigator.userAgent.indexOf("Win") != -1;
var MAC = navigator.userAgent.indexOf("Mac") != -1;
var LIN = navigator.userAgent.indexOf("Lin") != -1;

var M2 = (navigator.userAgent.indexOf("Mozilla/2") != -1);
var M3 = (navigator.userAgent.indexOf("Mozilla/3") != -1);
var M4 = (navigator.userAgent.indexOf("Mozilla/4") != -1);
var M5 = (navigator.userAgent.indexOf("Mozilla/5") != -1);

var OP = (navigator.userAgent.indexOf("Opera") != -1);
var OP3 = (navigator.userAgent.indexOf("Opera/3") != -1) && M3;
var OP35 = (navigator.userAgent.indexOf("Opera/3") != -1) && M4;
var OP36 = (navigator.userAgent.indexOf("Opera 3") != -1) && M4;
var OP4 = (navigator.userAgent.indexOf("Opera 4") != -1) ||
(navigator.userAgent.indexOf("Opera/4") != -1);
var OP5 = (navigator.userAgent.indexOf("Opera 5") != -1) ||
(navigator.userAgent.indexOf("Opera/5") != -1);
var OP6 = (navigator.userAgent.indexOf("Opera 6") != -1) ||
(navigator.userAgent.indexOf("Opera/6") != -1);
var OP7 = (navigator.userAgent.indexOf("Opera 7") != -1) ||
(navigator.userAgent.indexOf("Opera/7") != -1);

var IE = (navigator.userAgent.indexOf("MSIE") != -1) && !OP;
var IE3 = (navigator.userAgent.indexOf("MSIE 3") != -1);
var IE4 = (navigator.userAgent.indexOf("MSIE 4") != -1);
var IE50 = (navigator.userAgent.indexOf("MSIE 5.0") != -1) && !OP;
var IE55 = (navigator.userAgent.indexOf("MSIE 5.5") != -1) && !OP;
var IE60 = (navigator.userAgent.indexOf("MSIE 6.0") != -1) && !OP;

var NN = (navigator.appName == "Netscape") && !OP;
var NN2 = (navigator.appName == "Netscape") && M2 && !OP;
var NN3 = (navigator.appName == "Netscape") && M3 && !OP;
var NN4 = (navigator.appName == "Netscape") && M4 && !OP;
var NN6 = (navigator.userAgent.indexOf("Netscape6") != -1) && M5; && !OP;
var NN7 = (navigator.userAgent.indexOf("Netscape/7") != -1) && M5; && !OP;

var MOZ1 = M5; && (navigator.userAgent.indexOf("Netscape") == -1) && !OP;
&& (navigator.userAgent.indexOf("Gecko") != -1) &&
(navigator.userAgent.indexOf("rv:1") != -1)

//-->
</SCRIPT>

Script found at <URL:
http://www.xs4all.nl/~werksman/webmees/js_vb_browser_detectie.html > -- I
added Opera 7 to it.

You can use this, or a similar script, to test for any specific Opera
version, or just allow _any_ Opera version. Remember to use a fallback for
non-detected browsers, so people using the Spacemonkey 42 browser won't be
locked out by your sniffing.

Jernej Simončič

unread,
Jan 1, 2003, 10:55:41 AM1/1/03
to
On Wed, 1 Jan 2003 09:53:33 -0500, Tim Romano wrote:

> Maybe this elegant Opera-blessed browser detection code already exists
> and is prominently displayed on Opera's site somewhere? Searching the
> site halfheartedly did not discover it.

Here's a modification of BrowserSniffer script from cross-browser to detect
Opera 7, too:

// ClientSnifferJr Object Constructor
// Mike Foster, 12/12/01
// cross-browser.com

function ClientSnifferJr() {
this.ua = navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion);
this.minor = parseFloat(navigator.appVersion);
// DOM Support
if (document.addEventListener && document.removeEventListener) this.dom2events = true;
if (document.getElementById) this.dom1getbyid = true;
// Opera
this.opera = this.ua.indexOf('opera') != -1;
if (this.opera) {
this.opera5 = (this.ua.indexOf("opera 5") != -1 || this.ua.indexOf("opera/5") != -1);
this.opera6 = (this.ua.indexOf("opera 6") != -1 || this.ua.indexOf("opera/6") != -1);
this.opera7 = (this.ua.indexOf("opera 7") != -1 || this.ua.indexOf("opera/7") != -1);
return;
}
// Konqueror
this.konq = this.ua.indexOf('konqueror') != -1;
// MSIE
this.ie = this.ua.indexOf('msie') != -1;
if (this.ie) {
this.ie3 = this.major < 4;
this.ie4 = (this.major == 4 && this.ua.indexOf('msie 5') == -1 && this.ua.indexOf('msie 6') == -1);
this.ie4up = this.major >= 4;
this.ie5 = (this.major == 4 && this.ua.indexOf('msie 5.0') != -1);
this.ie5up = !this.ie3 && !this.ie4;
this.ie6 = (this.major == 4 && this.ua.indexOf('msie 6.0') != -1);
this.ie6up = (!this.ie3 && !this.ie4 && !this.ie5 && this.ua.indexOf("msie 5.5") == -1);
return;
}
// Misc.
this.hotjava = this.ua.indexOf('hotjava') != -1;
this.webtv = this.ua.indexOf('webtv') != -1;
this.aol = this.ua.indexOf('aol') != -1;
if (this.hotjava || this.webtv || this.aol) return;
// Gecko, NN4+, and NS6
this.gecko = this.ua.indexOf('gecko') != -1;
this.nav = (this.ua.indexOf('mozilla') != -1 && this.ua.indexOf('spoofer') == -1 && this.ua.indexOf('compatible') == -1);
if (this.nav) {
this.nav4 = this.major == 4;
this.nav4up= this.major >= 4;
this.nav5up= this.major >= 5;
this.nav6 = this.major == 5;
this.nav6up= this.nav5up;
}
}

window.is = new ClientSnifferJr();

--
Jernej Simončič, <jernej simoncic at guest arnes si>
http://www2.arnes.si/~sopjsimo/

Mike Foster

unread,
Jan 1, 2003, 4:45:29 PM1/1/03
to
Browser sniffing is a controversial topic.

Browser-detection and object-detection relate solely to the object model
exposed by the browser. Browser-detection makes assumptions about
implemented objects. Obect detection tests for their existence.

Theoretically, object-detection is the better technique. However, it is not
foolproof. Just because the same object exists in two different browsers
does not guarantee that their semantics are equivalent.

While developing my latest DHTML API (the X library) I endeavered to use
only object-detection. I was mostly successful, but I found a few cases
where browser-detection was required.

Finding clientWidth and clientHeight, and resolving certain event
properties, required browser-detection.

Here's the simple browser sniffing used by the X library:

//----------
var
xOpera56=false, xIE4Up=false,
xNN4=false, xUA=navigator.userAgent.toLowerCase();
if(window.opera){
xOpera56=(xUA.indexOf("opera 5")!=-1 || xUA.indexOf("opera/5")!=-1
|| xUA.indexOf("opera 6")!=-1 || xUA.indexOf("opera/6")!=-1);
}
else if(document.layers) xNN4=true;
else {xIE4Up=document.all && xUA.indexOf('msie')!=-1 &&
parseInt(navigator.appVersion)>=4;}
//----------

Since this is also being posted to the wishlist NG, I will say one more
thing...

O7 needs this:

window.opera.version

to easily differentiate between v5 or 6, and v7.

0 new messages