Capturing print events through jQuery

I would like to be able to manipulate the DOM just before sending my page for printing. Internet Explorer has an event in the window object called "onbeforeprint", but it is proprietary and not supported by other browsers. Can this be done using javascript (specifically jQuery, if possible)?

Before you ask, I cannot easily use the print stylesheet to apply the changes, because the elements I need to change have built-in styles that cannot be overridden by the global stylesheet. I need to override these inline print styles. It should be possible to modify existing jQuery if necessary, however it will be a more time-consuming and risky change.

Cheers zac

+3
source share
2 answers

Adding !importantafter a property in your CSS will allow it to override inline styles. For instance:

<div class="test" style="color: blue;">Some Text</div>

CSS

  .test {
     color: red !important;
  }

will be displayed in red.

+2
source

Why not just call print () from another function?

how

function myPrint() {
  $("#myDiv").css({"border-color":"red"});
  window.print();
}

Then you could call him where you need him.

+4
source

Source: https://habr.com/ru/post/1699690/


All Articles