admin管理员组

文章数量:1335179

Kind of an absurd situation... This is only really only for IE8 and lower.

Anyway I have an iFrame appearing (which I can't control / create an ID for), appreciate it Telerik!

// The only way I can grab it specifically would be:
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
                                             // ^ you grab by ID in IE
var ifWin = iframe.contentWindow || iframe;

But since I don't have the ID, I can't use this method... I've tried a few different methods, with no success.

// tried either window. / or document.
document.frames.print();
window.frames.print();
document.frames[0].print();

I basically just want to just, GRAB the first iFrame -> and print it

Side note: window.print() works in everything besides IE8 & lower. For IE, it prints only the outer / parent window content, instead of the iFrame that is currently focused on.

Any thoughts?


Update: So somehow doing window.frames[2].print() in the IE console works (and grabs the correct frame). When I try to put this code in the Javascript, I have the exact same thing: window.frames[2].print(), IE interprets this code into dot notation window.frames.2 and doesn't work and es back as null or not an object. Typing that same dot notation into the console doesn't work and just gives me "Expected ;" (which makes no sense).

Kind of an absurd situation... This is only really only for IE8 and lower.

Anyway I have an iFrame appearing (which I can't control / create an ID for), appreciate it Telerik!

// The only way I can grab it specifically would be:
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
                                             // ^ you grab by ID in IE
var ifWin = iframe.contentWindow || iframe;

But since I don't have the ID, I can't use this method... I've tried a few different methods, with no success.

// tried either window. / or document.
document.frames.print();
window.frames.print();
document.frames[0].print();

I basically just want to just, GRAB the first iFrame -> and print it

Side note: window.print() works in everything besides IE8 & lower. For IE, it prints only the outer / parent window content, instead of the iFrame that is currently focused on.

Any thoughts?


Update: So somehow doing window.frames[2].print() in the IE console works (and grabs the correct frame). When I try to put this code in the Javascript, I have the exact same thing: window.frames[2].print(), IE interprets this code into dot notation window.frames.2 and doesn't work and es back as null or not an object. Typing that same dot notation into the console doesn't work and just gives me "Expected ;" (which makes no sense).

Share Improve this question edited Oct 31, 2012 at 21:53 Mark Pieszak - Trilon.io asked Oct 31, 2012 at 21:28 Mark Pieszak - Trilon.ioMark Pieszak - Trilon.io 67.2k15 gold badges83 silver badges96 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Did you try

document.getElementsByTagName('iframe')[0]

I tested this in an IE8 console and it works (but it may still have the problem in your update).

As for the other part of your question about printing the first visible iframe, you probably will want to look at the scrollX and scrollY and the offsetTop and offsetLeft, no?

Most of these old collections in javascript has a method * called item, so try this:

window.frames.item(2).print();

(*) Do not wonder, typeof window.frames.item will be 'object'.

本文标签: javascriptGetting first visible iFrame within a window and printing itStack Overflow