...

View Full Version : How can I detect XHTML in the XML DOM? Is that even what I want?



brothercake
07-24-2003, 06: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 ..?

Vladdy
07-24-2003, 08:24 PM
Why not just test for method/property you are interested in?

beetle
07-25-2003, 04: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;
}
else
{
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;
default :
if ( isNamespaced )
{
elem.setAttributeNS( "http://www.w3.org/1999/xhtml", i, '' );
}
else
{
elem.setAttribute( i, '' );
}
elem[i] = attribs[i];
}
}
}
return elem;
}

brothercake
07-25-2003, 11:57 PM
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 = (
navigator.vendor!='KDE'&&
typeof document.childNodes!="undefined"&&
typeof document.all=="undefined"&&
typeof navigator.taintEnabled=="undefined"
)?true:false;
udm.xdom = (
typeof document.createElementNS!='undefined'&&
typeof document.styleSheets!="undefined"&&
!udm.safari
)?true:false;

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..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum