使用 Javascript 打印(部分)网页 [英] Printing a (part of) webpage with Javascript

查看:23
本文介绍了使用 Javascript 打印(部分)网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有两种调用浏览器打印"对话框的方法(当然我使用的是搜索):

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?

另一个问题:打印网页给定部分的最直接方式是什么?我知道我们可以创建新窗口或 iframe 来调用上述两种打印方法中的任何一种.哪个陷阱少?

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?

推荐答案

我已经测试了跨浏览器打印部分网页的不同方式:Chrome、Firefox、Opera(12 和新)、IE11、10、9 和 8.我尝试创建新的 window、新的 iframe,或使用 页面上现有的 iframe.然后尝试了 .print().execCommand('print').

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

注意:请记住,在 window 上调用 .print(),在 document 上调用 .execCommand().

Note: Keep in mind that .print() is called on window, and .execCommand() is called on document.

可以在此处

如果我的测试代码有误,请纠正我,我只是想找到最清晰的方法来完成这项工作.我的结论:

Correct me if my testing code is wrong, I just wanted to find the clearest way to do the job. My conclusions:

  • Opera 12 无法打印部分网页 (?)
  • IE 不print() iframe 和窗口,当前窗口除外.
  • 在 iframe 内的 documents 或在 IE 中创建的窗口上调用 print() 会破坏当前 document 上的 print()代码>.小心!
  • jQuery 插件 printThis 使用 IE 的技巧来完成这项工作,而且它确实有效.唯一的例外是 Opera 12.顺便说一下,这个插件使用了 print().
  • execCommand('print') 几乎可以在任何地方使用任何方法(iframe、窗口).但 Firefox 不支持它.
  • execCommand() 如果调用不成功则返回 false,所以如果你不想使用插件和魔术,创建 window 或 iframe,调用 execCommand('print'),如果返回false,调用print().
  • Opera 12 can not print part of a webpage (?)
  • IEs don't print() iframes and windows, except current window.
  • Calling print() on documents inside iframes or created windows in IEs breaks the print() on current document. 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() returns false if call was unsuccessful, so if you don't want to use plugins and magic tricks, create window or iframe, call execCommand('print') and if it returns false, call print().

还有一件事:

创建 iframe 很棘手:你不能直接访问它的 windowdocument(是的,你有 ContentDocument 属性,它的行为浏览器不同).您应该命名,然后调用window.frames[name] 从该iframe 中获取window 对象.不要尝试调用 window.frames(id) - 它会返回 iframe.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆