Lasse said:
The error is your assumption that you can go from a DOM element to
the window containing the DOM document of the element. The window object
is not a DOM object, so the DOM specification doesn't say how to find
it.
Rather unfortunate, that.
You might be able to use document.defaultView, as some browsers have
that property point to the window object. It is not specified that the
defaultView should also be the global object, or a window object, and
it's not available in all browsers, so it's not a good solution.
Thanks for the tip, I had missed it before. I just tested it out on my
Firefox 1.5 / Win XP Pro and it is working fine. Here is what I did:
Main page:
<html><head><title>Containing Page</title>
<script type='text/javascript'>
function onLoad() {
var iframe = document.getElementById('myframe');
var elemInFrame = iframe.contentDocument.getElementById('myElem');
var elemWindow = elemInFrame.ownerDocument.defaultView;
elemWindow.setTimeout ("document.getElementById('myspan').innerHTML =
\
'The answer is: mozilla'", 100);
}
</script>
</head><body onload="onLoad()">
This page contains a single iframe with
an image element to see if we can get to
that element's containing window<br><br>
<iframe id=myframe src="frame.htm"
style="height:4in;width:6in"></iframe>
</body></html>
Frame.htm:
<html><head><title>Frame Page</title></head>
<body>
This is the frame<br><br>
<span id=myspan style="border:1px solid green">
This text in the frame should get replaced</span><br><br>
<div>
<img id=myElem src="
https://www.mozilla.org/images/header_logo.gif">
</div>
</body></html>
The above test also works for IE 6, if I change the middle two lines of
script to:
var elemInFrame = iframe.Document.getElementById('myElem');
var elemWindow = elemInFrame.document.parentWindow;
Csaba Gabor from Vienna