Home  >  Article  >  Web Front-end  >  How to implement printing in JS (detailed answer combined with code)

How to implement printing in JS (detailed answer combined with code)

亚连
亚连Original
2018-05-18 10:31:051938browse

The following is how I compiled for you how to implement printing in JS. Interested students can take a look.

1. How JS implements printing

Method 1: window.print()

window.print(); A print dialog box will pop up, printing the content in window.document.body.innerHTML , the following is an example of partial printing taken from the Internet. The disadvantage of this example is that printing will modify the content of the page.

  
  
  
  
局部打印案例  
  

1不需要打印的地方

2这里不要打印啊

打印标题

打印内容~~

不打印的地方啊哈哈哈哈

2

[javascript] view plain copy 局部打印案例

1不需要打印的地方

2这里不要打印啊

打印标题

打印内容~~

不打印的地方啊哈哈哈哈

2

Method 2: Use the html tag 273238ce9338fbb04bee6997e5552b95 to introduce the Webbrowser control

This method is only compatible with IE, other browsers It cannot be used. At the same time, it can only be used by browsers below IE10. The calling method is as follows:

  
       
  
  
  
       
  

The advantage of this method is that a print preview can pop up under IE. This is a very user-friendly function for printing, but unfortunately Later versions of IE browsers do not support WebBrowser

Method 3: Use document.execCommand(”print”)

This method is also compatible Various versions of browsers, like window.print(), start the print dialog box. Chrome's print dialog box comes with a preview function, but IE and Firefox only pop up the print settings dialog box without a preview function.

The above is the method I compiled for you on how to implement printing in JS. I hope it will be helpful to you in the future.

Related articles:

About simple operations for downloading files in js (code attached, detailed answers)

function in js For questions about !, the code is attached

Detailed explanation of how JS interacts with the app (code attached)

The above is the detailed content of How to implement printing in JS (detailed answer combined with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn