joris
05-02-2006, 03:26 PM
Hi all,
I'm using Firefox 1.0.8, and try to replace innerHTML with a combination of DOMParser(), importNode, and replaceChild(). However, the XHTML code fragment I'm trying to insert contains a <table> element and seems to be imported incorrectly.
I've got the following XHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>FooBar</title>
<script type="text/javascript" src="foobar.js"></script>
</head>
<body onload="foobar();">
</body>
</html>
and here's foobar.js (note: this snippet uses appendChild instead of replaceChild).
<!--
function foobar()
{
var fragment, parser, parsed_fragment, node;
fragment = "<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><table id=\"foobar\"><tr><td>foo<\/td><\/tr><tr><td>bar<\/td><\/tr><\/table><\/div>";
parser = new DOMParser();
parsed_fragment = parser.parseFromString(fragment, "application/xhtml+xml");
node = document.importNode(parsed_fragment.documentElement, true);
document.body.appendChild(node);
alert(document.getElementById("foobar").rows.length);
}
//-->
Now, as the XHTML fragment defines a table with two rows, I would expect the alert() on the last line to show '2', but I got '0'. The DOM Inspector shows that the table has no tbody. However, the fragment is valid XHTML as far as I know, so shouldn't the tbody be generated automatically (like in any XHTML page)?
Any help would be appreciated.
Thanks,
Joris van Zwieten
I'm using Firefox 1.0.8, and try to replace innerHTML with a combination of DOMParser(), importNode, and replaceChild(). However, the XHTML code fragment I'm trying to insert contains a <table> element and seems to be imported incorrectly.
I've got the following XHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>FooBar</title>
<script type="text/javascript" src="foobar.js"></script>
</head>
<body onload="foobar();">
</body>
</html>
and here's foobar.js (note: this snippet uses appendChild instead of replaceChild).
<!--
function foobar()
{
var fragment, parser, parsed_fragment, node;
fragment = "<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><table id=\"foobar\"><tr><td>foo<\/td><\/tr><tr><td>bar<\/td><\/tr><\/table><\/div>";
parser = new DOMParser();
parsed_fragment = parser.parseFromString(fragment, "application/xhtml+xml");
node = document.importNode(parsed_fragment.documentElement, true);
document.body.appendChild(node);
alert(document.getElementById("foobar").rows.length);
}
//-->
Now, as the XHTML fragment defines a table with two rows, I would expect the alert() on the last line to show '2', but I got '0'. The DOM Inspector shows that the table has no tbody. However, the fragment is valid XHTML as far as I know, so shouldn't the tbody be generated automatically (like in any XHTML page)?
Any help would be appreciated.
Thanks,
Joris van Zwieten