views:

58

answers:

1

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?

A: 

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

Juan Mendes
window.frames doesn't work in IE7 when the "iframe" is really an `object` tag.
Scott Leis
I completely missed the fact tat you are using an object tag for an iframe. However, I just tested it using IE8 in IE7mode and it worked.I did it by going to http://intranation.com/test-cases/object-vs-iframe/, Added a name="ifrm" to the object tag, then used the console to check that I could indeed set window.frames.ifrm.location = "http://www.google.com"
Juan Mendes