How can I get contentWindow for an Object element in IE7
I have a HTML object element like this:
<object title="Search results as XML" standby="Loading XML document..." type="text/xml"
data="/__CA25761100195585.nsf/WebPrintListingXML?OpenAgent&date1=01/06/2009"
width="100%" height="100%" border="0" name="resultIFrame" id="resultIFrame">
Error: could not embed search results.
</object>
I also have this javascript function (alert() calls added for debugging):
function getFrameByName(fParent,fName)
{
var fArray=fParent.frames;
if (!fName) return;
if (fArray) {
if (fArray.length) {
for (var i=0; i<fArray.length; i++) {
alert('loop '+i);
if (fArray[i]) {
if (fArray[i].name==fName) return fArray[i];
}
}
}
}
var tmp=document.getElementsByName(fName);
if (tmp[0]) {
alert('returning '+tmp[0]);
if (!(tmp[0].contentWindow)) alert('contentWindow is null');
return tmp[0].contentWindow;
}
}
And finally, this button is meant to print the content of the Object element:
<input type="button" value="Print" name="printBtn"
onclick="getFrameByName(window,'resultIFrame').print();">
The button works perfectly in Firefox. Opera is good enough, though it prints the main document instead of just the object. IE7 gives the following error details:
Line: 57 Char: 1 Error: 'undefined' is null or not an objectLine 57 is where the button's "input" tag starts in the HTML source.
Thanks to the alert('contentWindow is 开发者_Python百科null')
call in the JS function, I know that the object I'm getting in IE has no contentWindow property.
I have tried changing the object
tag to an iframe
tag. This changes the JS behaviour, but causes other issues such as the height
attribute being ignored and the content not displaying.
object
tag, how can I get this Object's window in IE7?if you know the name of the iframe, the easiest way to get its window object is by calling window.frames[frameName]. This returns a reference to the window object, not the iframe
I know this question is old, but here are some of my findings:
The method using window.frames[frameName]
works fine in firefox and chrome, but contains a lot of wierd quirks in IE.
In ff and chrome you can access the documentelement by window.frames[frameName].document
, you only need to add the name attribute to the object tag.
But in IE(8 in my case) you need to call contentDocument
instead of document
. That's ok, but here is something really strange, if there is another totally unrelated object with an ID that equals the name of the object tag, IE gets confused and returns an html collection instead, containing both the ID-element and the named object. You then need to filter out the object from the list to access the contentDocument property using an index like so: window.frames[frameName][1].contentDocument
You can get the content of the document using jquery: $(window.frames[frameName].document).contents().find("selector");
Whew, so far this is all I have got. I think the best way is checking if the 'frame' contains a property 'contentDocument' for IE and otherwhise access the normal 'document' property.
精彩评论