Javascript Print Specific Part Of Webpage Stack Overflow

Dr. Aris Thorne
-
javascript print specific part of webpage stack overflow

Print only the content of a webpage using CSS and JavaScript Last updated: December 14, 2022. For security reasons, modern browsers do not allow JavaScript access to a user’s printer directly. But what JavaScript can do is make a request to print the current page, which prompts the user with a print preview.

Table of contents Print an entire page Printing couldn’t be much more straightforward: call window.print() or, because it is a method on the global object, just print(): /* Request to print the current page */ window.print(); // OR: print(); This will request to print the entire page. Print part of a web page Printing part of a page is more difficult because window.print() doesn’t accept any arguments that could specify a part of the page to print (e.g. a only).

Luckily, it is possible to specify the appearance of elements when printing using a CSS media query for the print view. So inside @media print {}, first specify that all elements inside the body should not be displayed in print view using the ‘all’ wildcard (* ). Afterwards, set the appearance of the element you want to print and its content to display: block, making it visible. Now, when window.print() is called, only the visible element will appear in the preview: Print me. Don't print me.

Print Assigning a print area with JavaScript In the above example, the element for printing has the print-area class name assigned to it. In practice, this may not already be the case in your HTML markup. But you can assign this name to any element using JavaScript. To do so, access the classList of an element and call the add() method, passing in the class name to add: For example, the code below assigns the class name print-area to an element with the ID of the-content: Print me.

Don't print me. Print Now, when window.print() is called, the.print-area CSS applies to the with ID the-content, and it (and not the ) is visible in the print view. Summary JavaScript can be used to send a job to the printer for user approval. An entire web page can be sent for printing by simply calling the window.print() method. To print part of a page, use a CSS media query for the print view, specifying that only HTML content you want to print should be displayed in this view.

Print%20Only%20The%20Content%20Of%20A%20Webpage%20Using%20CSS%20And%3F

Print%20only%20the%20content%20of%20a%20webpage%20using%20CSS%20and%20JavaScript%20Last%20updated%3A%20December%2014%2C%202022.%20For%20security%20reasons%2C%20modern%20browsers%20do%20not%20allow%20JavaScript%20access%20to%20a%20user%u2019s%20printer%20directly.%20But%20what%20JavaScript%20can%20do%20is%20make%20a%20request%20to%20print%20the%20current%20page%2C%20which%20prompts%20the%20user%20with%20a%20print%20preview.

Table%20Of%20Contents%20Print%20An%20Entire%20Page%20Printing%20Couldn%u2019t%20Be%3F

Table%20of%20contents%20Print%20an%20entire%20page%20Printing%20couldn%u2019t%20be%20much%20more%20straightforward%3A%20call%20window.print%28%29%20or%2C%20because%20it%20is%20a%20method%20on%20the%20global%20object%2C%20just%20print%28%29%3A%20/*%20Request%20to%20print%20the%20current%20page%20*/%20window.print%28%29%3B%20//%20OR%3A%20print%28%29%3B%20This%20will%20request%20to%20print%20the%20entire%20page.%20Print%20part%20of%20a%20web%20page%20Printing%20part%20of%20a%20page%20is%20more%20difficult%20because%20window.print%28%29%20doesn%u2019t%20accept%20any%20argum...

Luckily%2C%20It%20Is%20Possible%20To%20Specify%20The%20Appearance%20Of%20Elements%3F

Luckily%2C%20it%20is%20possible%20to%20specify%20the%20appearance%20of%20elements%20when%20printing%20using%20a%20CSS%20media%20query%20for%20the%20print%20view.%20So%20inside%20@media%20print%20%7B%7D%2C%20first%20specify%20that%20all%20elements%20inside%20the%20body%20should%20not%20be%20displayed%20in%20print%20view%20using%20the%20%u2018all%u2019%20wildcard%20%28*%20%29.%20Afterwards%2C%20set%20the%20appearance%20of%20the%20element%20you%20want%20to%20print%20and%20its%20content%20to%20display%3A%20block%2C%20making%20it%20visible.%20Now%2C%20when%20window.p...

Print%20Assigning%20A%20Print%20Area%20With%20JavaScript%20In%20The%20Above%3F

Print%20Assigning%20a%20print%20area%20with%20JavaScript%20In%20the%20above%20example%2C%20the%20element%20for%20printing%20has%20the%20print-area%20class%20name%20assigned%20to%20it.%20In%20practice%2C%20this%20may%20not%20already%20be%20the%20case%20in%20your%20HTML%20markup.%20But%20you%20can%20assign%20this%20name%20to%20any%20element%20using%20JavaScript.%20To%20do%20so%2C%20access%20the%20classList%20of%20an%20element%20and%20call%20the%20add%28%29%20method%2C%20passing%20in%20the%20class%20name%20to%20add%3A%20For%20example%2C%20the%20code%20below...

Don%27t%20Print%20Me.%20Print%20Now%2C%20When%20Window.print%28%29%20Is%20Called%2C%20The.print-area%3F

Don%27t%20print%20me.%20Print%20Now%2C%20when%20window.print%28%29%20is%20called%2C%20the.print-area%20CSS%20applies%20to%20the%20with%20ID%20the-content%2C%20and%20it%20%28and%20not%20the%20%29%20is%20visible%20in%20the%20print%20view.%20Summary%20JavaScript%20can%20be%20used%20to%20send%20a%20job%20to%20the%20printer%20for%20user%20approval.%20An%20entire%20web%20page%20can%20be%20sent%20for%20printing%20by%20simply%20calling%20the%20window.print%28%29%20method.%20To%20print%20part%20of%20a%20page%2C%20use%20a%20CSS%20media%20query%20for%20the%20print%20view%2C%20...