KayosIII
03-11-2003, 12:33 PM
Is this possible -- can somebody give me a code snippit to detect if a browser supports DOM level 1
|
||||
Detecting DOM Support in a browser....KayosIII 03-11-2003, 12:33 PM Is this possible -- can somebody give me a code snippit to detect if a browser supports DOM level 1 brothercake 03-11-2003, 01: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; liorean 03-11-2003, 05:10 PM try testing for document.implementation brothercake 03-11-2003, 05:29 PM Originally posted by liorean try testing for document.implementation Much better :thumbsup: But isn't that DOM2 ? liorean 03-11-2003, 06:27 PM Nope! See <http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/ecma-script-language-binding.html> brothercake 03-11-2003, 06:49 PM cool :) jkd 03-11-2003, 09: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. jkd 03-11-2003, 09: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'); liorean 03-11-2003, 09: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'); brothercake 03-11-2003, 09: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 :) liorean 03-11-2003, 10: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). brothercake 03-11-2003, 10:20 PM Excellent; thanks. I shall have to run some tests .. :) liorean 03-11-2003, 10:47 PM Well, please notify me of any results you find - I'm interested in how much I can use this. brothercake 03-11-2003, 11:00 PM 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 brothercake 03-12-2003, 09: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) liorean 03-12-2003, 01: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? brothercake 03-12-2003, 01:34 PM I'm not at my computer right now, but I'll try those out later on. brothercake 03-13-2003, 01: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) liorean 03-13-2003, 07:55 AM Have you reported the anomaly/bug to the opera crowd? brothercake 03-13-2003, 09:35 AM Originally posted by liorean Have you reported the anomaly/bug to the opera crowd? Yeah, soon as I found it. liorean 03-18-2003, 11:38 PM 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? liorean 04-26-2003, 01: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? brothercake 04-26-2003, 01:46 AM Yeah I reckon; it'll have to be tomorrow though ... brothercake 04-26-2003, 05:35 PM http://www.brothercake.com/Ref/in.html 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! liorean 04-26-2003, 05: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