使用Javascript打印(部分)网页 [英] Printing a (part of) webpage with Javascript
问题描述
-
document.print()
- document.execCommand('print',false,null)
他们之间有什么区别?支持跨浏览器?论文,文档或标准?哪一个更正确?
另一个问题是:什么是最直接的方式来打印给定的网页部分?我知道我们可以创建新窗口或iframe来调用上面两种打印方法中的任何一种。哪一个有更少的陷阱?解析方案
我已经测试了跨浏览器打印部分网页的不同方式:
Chrome ,Firefox,Opera(12以及新版本),IE11,10,9和8等。我试图创建新的窗口,新的
iframe
,或者在页面上使用
现有的iframe
。然后尝试 .print()
和 .execCommand('print')
。注意:请记住在窗口上调用.print(),并在上调用.execCommand() >文档 。
用于测试的代码可以在这里
如果我的测试代码错误,请纠正我的错误,我只是想找到完成这项工作的最明确的方法。我的结论:
- Opera 12不能打印网页的一部分(?)
- 't
print()
iframe和窗口,当前窗口除外。 在IE中的iframe或创建的窗口内的on 文档
打破当前<$ c $的print()
C>文档。注意!
- jQuery插件 printThis 使用IE来做这项工作的技巧,并且它只是工作。唯一的例外是Opera 12.顺便说一下,这个插件使用
print()
。
execCommand 'print')
几乎可以在任何地方以任何方式工作(iframes,window)。它不支持Firefox。
execCommand()
返回false
if调用不成功,所以如果你不想使用插件和魔术,创建窗口或iframe,请调用execCommand('print')
,如果返回false
,调用print()
。
- 't
还有一件事:
创建 iframe
非常棘手:您无法访问其直接显示窗口或文档(是的,您拥有ContentDocument属性,在浏览器中的行为不同)。您应为 ,然后调用 window.frames [name]
来从该iframe中获取 window 对象。不要尝试调用 window.frames(id)
- 它会返回 iframe 。
I am aware of two ways of calling the "print" dialog of browser (I used the Search, of course):
- document.print()
- document.execCommand('print', false, null)
What is the difference between them? Support across browsers? Papers, docs or standards? Which is more correct thing to use?
Another question: what is the most straight way to print given part of a webpage? I know we can create new window or iframe to call any of two print methods above. Which one has less pitfalls?
I've tested different ways of printing part of webpage across browsers:
Chrome, Firefox, Opera (12 and new), IE11, 10, 9 and 8. I've tried to create new window
, new iframe
, or use existing iframe
on the page. And then tried .print()
and .execCommand('print')
.
Note: Keep in mind that .print() is called on window, and .execCommand() is called on document.
Code used for testing can be found here
Correct me if my testing code is wrong, I just wanted to find the clearest way to do the job. My conclusions:
- Opera 12 can not print part of a webpage (?)
- IEs don't
print()
iframes and windows, except current window. - Calling
print()
ondocuments
inside iframes or created windows in IEs breaks theprint()
on currentdocument
. Be careful! - jQuery plugin printThis uses tricks for IE to do the job, and it just works. The only exception is Opera 12. By the way, this plugin uses
print()
. execCommand('print')
works almost everywhere and with any approach (iframes, window). It's not supported by Firefox though.execCommand()
returnsfalse
if call was unsuccessful, so if you don't want to use plugins and magic tricks, create window or iframe, callexecCommand('print')
and if it returnsfalse
, callprint()
.
One more thing:
Creating an iframe
is tricky: you can't access its window or document directly (yes, you have ContentDocument property, which behaves differently across browsers). You should name it and then call window.frames[name]
to get window object from that iframe. Do not try to call window.frames(id)
- it will return the iframe.
这篇关于使用Javascript打印(部分)网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!