View Full Version : How can I detect XHTML in the XML DOM? Is that even what I want?
07-24-2003, 07:35 PM
For a script where it detects, and uses appropriate methods, so that it works on XHTML pages regardless of which DOM it's in.
At the moment, I'm testing for document.createElementNS and document.styleSheets, but it's messy - Safari declares support for both but the DOM 2 CSS doesn't actually work ... IE supports document.styleSheets but it uses a proprietary rule syntax ... Opera 7 doesn't support that but it does support createElementNS.
So instead of testing for support with named-browser exclusions, I thought maybe test for the XML DOM ... but then is it safe to assume that any JS-capable browser which is in the XML DOM supports the required methods?
Or is this all bunk ... should I just be pragmatic and test for Gecko ..?
07-24-2003, 09:24 PM
Why not just test for method/property you are interested in?
07-25-2003, 05:34 PM
Agreed, that's how I handle createElementNS
document.createHTMLElement = function( elemName, attribs )
if ( document.createElementNS )
var elem = document.createElementNS( "http://www.w3.org/1999/xhtml", elemName );
var isNamespaced = true;
var elem = document.createElement( elemName );
var isNamespaced = false;
if ( typeof attribs != 'undefined' )
for ( var i in attribs )
switch ( true )
case ( i == 'text' ) : elem.appendChild( document.createTextNode( attribs[i] ) ); break;
case ( i == 'class' ) : elem.className = attribs[i]; break;
if ( isNamespaced )
elem.setAttributeNS( "http://www.w3.org/1999/xhtml", i, '' );
elem.setAttribute( i, '' );
elem[i] = attribs[i];
07-26-2003, 12:57 AM
Yeah that's essentially what I'm doing now, but to save code in disparate places where one or the other method is needed, I wanted to set a global flag that means it understands both document.styleSheets and createElementNS
What I'm currently do is this, and it works just fine:
udm.safari = (
udm.xdom = (
But it's not 100% reliable - because the Safari test is a hack that may not always work - and it doesn't allow for the possibility of future browsers which may, like Safari, declare support for both but not actually have a useable implementation.
So I was thinking, if I *know* a document is in the XML DOM, it's near-as-dammit safe to assume it can support these methods.
But thanks guys for your answers. :) I wouldn't mind knowing academically how to detect the DOM, but I guess it's not essential to know..
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.