View Full Version : Detecting DOM Support in a browser....

03-11-2003, 01:33 PM
Is this possible -- can somebody give me a code snippit to detect if a browser supports DOM level 1

03-11-2003, 02:04 PM
generally, document.getElementById and document.createElement are useful object tests for this.

However, Opera 6 returns a function for createElement, even though it can't add the created element to the page. So to cater for that, I'd do something like this:

var agt = navigator.userAgent.toLowerCase();
var op6 = (agt.indexOf("opera/6")!=-1||agt.indexOf("opera 6")!=-1)?true:false;

var DOM = (typeof document.getElementById!="undefined" && typeof document.createElement!="undefined" && !op6)?true:false;

03-11-2003, 06:10 PM
try testing for document.implementation

03-11-2003, 06:29 PM
Originally posted by liorean
try testing for document.implementation

Much better :thumbsup: But isn't that DOM2 ?

03-11-2003, 07:27 PM
Nope! See <http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/ecma-script-language-binding.html>

03-11-2003, 07:49 PM
cool :)

03-11-2003, 10:19 PM
var dom1 = document.implementation && document.implementation.hasFeature('XML', '1.0') && document.implementation.hasFeature('HTML', '1.0');

That should be pretty accurate, if vendors do what they are supposed to.

03-11-2003, 10:21 PM
Might be a good idea to remove the DOM1 XML check, as IE is stupid... and apparently so is Opera. hmph

var dom1 = document.implementation && document.implementation.hasFeature('HTML', '1.0');

03-11-2003, 10:29 PM
Hey - using strict warnings?

try this

var dom1=('implementation' in document)&& document.implementation.hasFeature('HTML', '1.0');

or, a bit longer but more backwards compatible:

var dom1=(typeof document.implementation!='undefined')&& document.implementation.hasFeature('HTML', '1.0');

03-11-2003, 10:39 PM
Originally posted by liorean
var dom1=('implementation' in document)

Hey that's interesting. I didn't realise you could use in like that :)

03-11-2003, 11:16 PM
Yeah, I know. I've only seen it used internally in Mozilla/Netscape code before I started using it. I have no idea how the support for it is, except that op7, moz and jscript5.6 (comes with ie6, and a few other microsoft products) support it.

DevEdge JavaScript 1.5 Core Reference (http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ops.html#1066286) says it was introduced in JavaScript 1.4.

MSDN Library Scripting JScript (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoprin.asp) says it was introduced in JScript 1.0 (ie3).

03-11-2003, 11:20 PM
Excellent; thanks. I shall have to run some tests .. :)

03-11-2003, 11:47 PM
Well, please notify me of any results you find - I'm interested in how much I can use this.

03-12-2003, 12:00 AM
I did a couple of different tests - one using implementation, and one using the images collection, like a control.

The results are mostly what you'd expect, but a couple of surprises in there:

var DOM = ("implementation" in document);

* moz (tested ns6.01 and moz1.3b): true
* netscape 3-4: error
* opera 7: false ... the first time you view the page in each Opera session, but true on refresh or if you visit it again in the same session
* opera 4-6: false
* ie6: true
* ie4-5: error
* safari: true

var DIM = ("images" in document);

* moz (ditto): true
* netscape 3-4: error
* opera 5-7: false
* opera 4: true!
* ie6: true
* ie4-5: error
* safari: true

You can see the test page at http://www.brothercake.com/Ref/in.html

03-12-2003, 10:59 AM
A couple of things that have come up on further testing:

- ("implementation" in document) returns true for the mac version of Opera 6; but presumably you can't actually use it.

- given the strange O7 results, I did another set of tests using (typeof document.implementation). The results are interesting:

* moz (ns6.01 and moz1.3b): [object DOMImplementation]
* netscape 3-4: undefined
* opera 7: [object HTMLDOMImplementation]
* opera 4-6: undefined .. except:
* opera 6/mac: [object DOMImplementation] (but presumably you can't actually use it)
* opera 3: null
* ie6: [object]
* ie5/mac: [object Implementation] (?)
* ie5/win: undefined
* ie4: undefined
* safari: [object DOMImplementation]

What this suggest to me is that, as an object test for DOM1 support, document.implementation has some caveats -

1 - it's undefined in win/ie5 - which may or may not be appropriate

2 - it returns an object collection in the mac version of Opera 6, but presumably you can't actually use it.

3 - it returns null (rather than "undefined") in Opera 3

Updated test results (http://www.brothercake.com/Ref/in.html)

03-12-2003, 02:19 PM
Wow- what a mess.
If you test also using document.implementation.hasFeature('HTML','1.0'), or even just testing for ('hasFeature' in document.implementation), what does that say?

03-12-2003, 02:34 PM
I'm not at my computer right now, but I'll try those out later on.

03-13-2003, 02:34 AM
Interesting ... ("hasFeature" in document.implementation); is similar to ("implementation" in document); - same anomoly in O7, but additionally creates errors in opera 4,5 and 6

Adding document.implementation.hasFeature('HTML','1.0')
to the typeof test produces consistent results, except for opera 3 which fails to return instead of returning null, and mac/opera 6 which returns false (thankfully!)

check it out (http://www.brothercake.com/Ref/in.html)

03-13-2003, 08:55 AM
Have you reported the anomaly/bug to the opera crowd?

03-13-2003, 10:35 AM
Originally posted by liorean
Have you reported the anomaly/bug to the opera crowd?

Yeah, soon as I found it.

03-19-2003, 12:38 AM
Hmm, is there a difference if you apply it to user-created objects? I think I've seen such a difference in the for...in loop.

Oh, why didn't anybody add the useful:
object.each() {|i| do-somwthing-with-i-here} syntax from ruby to javascript?

04-26-2003, 02:26 AM
Hmm, do you think you could add tests on user created objects to that test page, brothercake? How do they behave compared to host objects?

04-26-2003, 02:46 AM
Yeah I reckon; it'll have to be tomorrow though ...

04-26-2003, 06:35 PM

I addd this test to it:

var myObj = new Object;
myObj.foo = "bar";
var FIM = ("foo" in myObj);

the results are what you'd expect, except for Opera - it returns true all the way to version 4!

04-26-2003, 06:44 PM
It was as I suspected then - for..in worked on user created objects before host objects in ie - same is the case for [string] in [object] in opera.

I hope they fix this soon as op7 and ie5m are the only true gen5 browsers not supporting it.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum