javascript - Getting first visible iFrame within a window and printing it - Stack Overflow

Kind of an absurd situation... This is only really only for IE8 and lower.Anyway I have an iFrame appea

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'.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742382711a4433438.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信