The mysterious Opera object

The Opera object?

Yes the window.opera object. This is a script object that Opera has provided since Opera 5. It is provided to aid debugging of scripts.

Many real-world scripts use window.opera to check if the browser is Opera. However, very few of them actually seem to use the properties or methods that are attached to this object.

Opera documents many of these itself in its jsconsole.html, but since these are not on the web, I thought I would put them there. Some of these docs are shamelessly ripped from that file.

Since the Opera object is not part of any standard, you should not rely on any of the properties or methods to exist in future releases, with the exception of the properties that Opera themselves have documented publically: postError, addEventListener, removeEventListener, defineMagicFunction, defineMagicVariable.

What the object provides

Using the opera object

Because the Opera object is an extension to JavaScript, no other browser supports it. Therefore, it is important to test for its existence before you try to use it:

if( window.opera ) {
	alert( opera.buildNumber('inconspicuous') );
}

However, just to be awkward, older versions of Opera do not support all of the methods of the Opera object so you need to test for these as well. Opera 5 supports none of the methods (since that leaves very little reason why they would bother to include the Opera object - with the exception of aiding browser sniffing, I wonder if I missed something ...). Opera 6 only supports buildNumber, and even then, only if you pass it a special string.

if( window.opera ) {
	if( opera.buildNumber ) {
		alert( opera.buildNumber('inconspicuous') );
	}
}

Don't forget that some of the methods can only be run from file: URLs, and some can only be run from user JavaScript files.

See Opera's developer article for information on how to use the postError method to debug scripts.

See Opera's User JavaScript tutorial (written by yours truly) for information on how to use the very powerful User JavaScript features.

Disabling it

Yes, there are some sites written by ignorant authors who think that opera should be blocked from entering their sites, almost always because they use DOM based scripts that Opera 6 and below could not support (but Opera 7+ can), so they block Opera by detecting the window.opera object (if all they do is use a userAgent detect, you might find it helps to change the 'Identify as ...' option in Tools - quick preferences, or for server-side User-Agent detects, you can also use the '4' or '5' values in ua.ini). Since Opera 8 you can use a simple User JavaScript to prevent scripts on the page from accessing it:

if( location.hostname.indexOf('badsite.example.com') + 1 ){
	window.opera.addEventListener('BeforeScript',function (e) {
		e.element.text = e.element.text.replace(/opera/ig,'borkme');
	},false);
	window.opera.addEventListener('BeforeJavaScriptURL',function (e) {
		e.source = e.source.replace(/opera/ig,'borkme');
	},false);
	window.opera.addEventListener('BeforeEventListener',function (e) {
		var asStr = e.listener.toString();
		if( asStr.match(/^function\s*\(\s*\)/) ) {
			e.listener.toString().replace(/opera/ig,'borkme');
			e.listener = new Function(asStr.replace(/^function\s*\(\s*\)\s*{|}\s*$/g,''));
		}
	},false);
}

Or you can use User JavaScript to completely disable it.

Older versions of Opera do not actually offer a way to hide this object, so they have no defense against these scripts. I recommend upgrading if possible. However, not to be defeated, there are two possibilities if you cannot upgrade.

Back to Opera resources | Back to How To Create