yawnmoth said:
You mean browser? I don't see why the User-Agent part of the http
request would have any influence over this..
User-agent is a generic term for software that accesses documents on the
web. This includes browsers, and that's what I was referring to.
I'm just trying to get a better understanding of what importNode
does...
When the user-agent loads a document, it checks the document's content
type. If that content type is HTML or XML, the user agent *should*
parse it and create a DOM tree consisting of a node hierarchy.
importNode takes a node from a different document's DOM tree and makes
it available to the current document.
See the W3 DOM specification for more info:
Anyway, here's another example that doesn't seem to do what I'd expect
(they're both hosted on 127.0.0.1):
test.htm:
<body id="container">
<iframe src="
http://127.0.0.1/test.htm"></iframe>
</body>
<script>
var iframe = document.getElementsByTagName("iframe")[0];
var oldNode = iframe.contentDocument.getElementById("myNode");
var newNode = document.importNode(oldNode,true);
document.getElementById("container").appendChild(newNode);
</script>
There are a lot of problems with this example. First of all, neither of
your documents are valid HTML, and I would not expect anything to work
properly if the documents in question are not valid. Second, even if
the documents were successfully parsed, you would be trying to append a
<body> element into a <body> element. Third, even if the script in
test.htm gets executed (which it shouldn't, since it's outside the
<body> and non-existent <head>), there is no guarantee that the contents
of the iframe have been loaded.
And fourth, I'm not sure if this is a typo or not, but test.htm's iframe
is pointing to test.htm rather than test2.htm.
test2.htm:
<body id="myNode">
hello, world!
</body>
I'd expect "hello, world" to appear twice on the page - once in the
iframe and once outside of it. Yet it only appears once...
Try this.
test.htm:
<html>
<head>
<title>My DOM Test</title>
</head>
<body>
<iframe id="myIFrame" src="test1.htm"></iframe>
<script type="text/javascript">
function loadMyNode()
{
var iframeDoc = (iframe.contentWindow || iframe.contentDocument);
if(iframeDoc.document)
iframeDoc = iframeDoc.document;
if(!iframeDoc)
return false;
var myNode = document.importNode(iframeDoc.getElementById("myNode"),
true);
if(myNode)
document.body.appendChild(myNode);
else return false;
}
var iframe = document.getElementById("myIFrame");
try
{
if(!loadMyNode())
iframe.onload = loadMyNode;
}
catch(e)
{
iframe.onload = loadMyNode;
}
</script>
</body>
</html>
test1.htm:
<html>
<head>
<title>External Document</title>
</head>
<body>
<p id="myNode">Hello, world!</p>
</body>
</html>
Works for me.
Jeremy