PDA

View Full Version : Sniff for Opera 7 and up


Graeme Hackston
12-18-2002, 05:03 AM
I downloaded Opera 7, beta 2 from here:

ftp://ftp.opera.com/pub/opera/win/700/

It appears to be fully functional with the DOM after page load. Is there a better method of siniffing for version 7 and up than I've posted below? Something that wouldn't require updating the array in the future?

VerNum = new Array('5','6','7','8')
for (var i=0;i<VerNum.length;i++){
if(navigator.userAgent.toLowerCase().indexOf("opera " + VerNum[i])!=-1){
if(i>1){
// do DOM stuff
}
}
}

Skyzyx
12-18-2002, 05:45 AM
Well, personally, this is how I sniff for Opera while leaving my options open for the future...


// DECLARE GLOBAL VARIABLES
var browser = new Array();
var ua=navigator.userAgent;

// IS THIS OPERA?
browser.opera=(ua.indexOf('Opera') != -1) ? true:false;
browser.ver=0;

// DETECT OPERA REGARDLESS OF IDENTITY
if (ua.indexOf('Opera ') != -1)
{
browser.split=ua.split('Opera ');
browser.ver=parseInt(browser.split[1]);
}
else if (ua.indexOf('Opera/') != -1)
{
browser.split=ua.split('Opera/');
browser.ver=parseInt(browser.split[1]);
}

// IS OPERA THIS VERSION?
browser.opera3=(browser.opera && browser.ver == 3) ? true:false;
browser.opera4=(browser.opera && browser.ver == 4) ? true:false;
browser.opera5=(browser.opera && browser.ver == 5) ? true:false;
browser.opera6=(browser.opera && browser.ver == 6) ? true:false;
browser.opera7=(browser.opera && browser.ver == 7) ? true:false;
browser.opera8=(browser.opera && browser.ver == 8) ? true:false;
browser.opera9=(browser.opera && browser.ver == 9) ? true:false;

// IS OPERA THIS VERSION OR NEWER?
browser.opera3up=(browser.opera && browser.ver >= 3) ? true:false;
browser.opera4up=(browser.opera && browser.ver >= 4) ? true:false;
browser.opera5up=(browser.opera && browser.ver >= 5) ? true:false;
browser.opera6up=(browser.opera && browser.ver >= 6) ? true:false;
browser.opera7up=(browser.opera && browser.ver >= 7) ? true:false;
browser.opera8up=(browser.opera && browser.ver >= 8) ? true:false;
browser.opera9up=(browser.opera && browser.ver >= 9) ? true:false;

Graeme Hackston
12-19-2002, 12:34 AM
Thanks Skyzyx. Your code was helpful. I'm just doing minimal sniffing at present. What I need is a light method of excluding version 6 and below from a DOM js file.

I think (as usual) I was coming at it backwards. I believe this will do what I need if I find out, and fill the array with, all the previous version numbers.

if(window.opera){
VerNum = new Array('3','4','5','6')
for (var i=0;i<VerNum.length;i++){
if((navigator.userAgent.toLowerCase().indexOf("opera " + VerNum[i])!=-1)||(navigator.userAgent.toLowerCase().indexOf("opera/" + VerNum[i])!=-1)){
} else {
//read DOM file
}
}
}

Graeme Hackston
12-19-2002, 12:52 AM
Ok, I'm not sure why but this works and the above doesn't. Probably something to do with an else in a loop.

if(window.opera){
VerNum = new Array('3','4','5','6')
for (var i=0;i<VerNum.length;i++){
if((navigator.userAgent.toLowerCase().indexOf("opera " + VerNum[i])!=-1)||(navigator.userAgent.toLowerCase().indexOf("opera/" + VerNum[i])!=-1)){
//don't read DOM file
}
}
}

whammy
12-19-2002, 01:08 AM
I think the whole "browser/version" HTTP Header information needs to be standardized as well. It's pretty pathetic the way it stands now, since it looks like what happens when many birds decide to "release" themselves on your vehicle as it's parked in your driveway.

Anyone else wanna lobby for this as a standard? Not only in the HTTP Header, which would be awesome because it could be parsed server-side, but (at the very least) in ECMAScript?

I know it would make all of our lives much simpler if there was an "easy" way to find out EXACTLY (not vague references to a gecko or mozilla engine which may or may not be accurate) which browser was being used on the client-side using either client-side OR a server-side script, by reading the header...

The above posts should illustrate the problem in plenty of detail to elucidate what I'm getting at here. There's no excuse for having to code for that...

:D

Graeme Hackston
12-19-2002, 01:18 AM
I agree whammy. What is this “opera + forward slash” thing anyway? Hopefully before long they’ll all render the same and sniffing won’t be needed.

whammy
12-19-2002, 01:19 AM
I think we need to get together and lobby the ECMA or somebody for this as a standard, because it's going nowhere fast! And it's a pain in the butt. It would be a LOT easier to use (at least server-side, hopefully client-side as well) if this information conformed to SOME kind of standard. No more "Hey, my browser is IE 6, but it masks as Mozilla whatever"... since the browsers have some proprietary garbage thrown in or different support for CSS, etc. :(

Let's lobby for a CLEAR standard here... it should be obvious what's needed to any programmer.

As it is, it's impossible to parse (maybe not impossible, but I just don't have the time to devote to figuring it out, and because it's like bird droppings it's way too much trouble!). I gave up on it... it needs to be standardized!

No use "hoping" they will all "render the same soon" because apparently noone who is creating/distrubuting browser code is paying attention to this aspect. :(