How can I get contentWindow for an Object element in IE7

Posted by Scott Leis on Stack Overflow See other posts from Stack Overflow or by Scott Leis
Published on 2010-05-12T05:43:32Z Indexed on 2010/05/19 18:30 UTC
Read the original article Hit count: 287

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 object

Line 57 is where the button's "input" tag starts in the HTML source. Thanks to the alert('contentWindow is 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.
Sticking with an object tag, how can I get this Object's window in IE7?

© Stack Overflow or respective owner

Related posts about html

Related posts about JavaScript