JavaScript print Occasions

Media queries present a good way to programmatically change conduct relying on viewing state. We are able to goal kinds to machine, pixel ratio, display dimension, and even print. That stated, it is also good to have JavaScript occasions that additionally enable us to vary conduct. Do you know you are supplied occasions each earlier than and after printing?

I’ve all the time used @media print in stylesheets to regulate print show, however JavaScript supplies beforeprint and afterprint occasions:

operate toggleImages(disguise = false) {
  doc.querySelectorAll('img').forEach(img => { = disguise ? 'none' : '';

// Disguise photos to avoid wasting toner/ink throughout printing
window.addEventListener('beforeprint', () => toggleImages(true))
window.addEventListener('afterprint', () => toggleImages());

It could sound bizarre however contemplating print is essential, particularly when your web site is documentation-centric. In my early days of internet, I had a shopper who solely “considered” their web site from print-offs. Styling with @media print is often one of the best choices however these JavaScript occasions could assist!

  • An Interview with Eric Meyer

    Your early CSS books have been instrumental in pushing my love for entrance finish applied sciences. What was it about CSS that you just fell in love with and drove you to write down about it? At first blush, it was the simplicity of it as in comparison with the table-and-spacer…

  • How to Create a RetroPie on Raspberry Pi – Graphical Guide

    In the present day we get to play wonderful video games on our tremendous powered recreation consoles, PCs, VR headsets, and even cellular gadgets.  Whereas I take pleasure in taking part in new video games as of late, I do lengthy for the retro gaming methods I had after I was a child: the unique Nintendo…

  • Using MooTools ScrollSpy to Load More Items via JSON/AJAX
  • QuickBoxes for Dojo

    Including to my psychological portfolio is necessary to me. First got here MooTools, then jQuery, and now Dojo. I converse typically with Peter Higgins of Dojo fame and determined it was time to step into his world. I selected a easy however helpful plugin…