Friday, May 17, 2024
HomeTechnologySoftwareJavaScript print Events

JavaScript print Events

Date:

Related stories

Fortnite Announces Surprise Fallout Crossover

Bethesda’s post-apocalyptic RPG franchise, Fallout, is coming to...

Samsung Galaxy Z Fold6 appears on Geekbench

The Samsung Galaxy Z Fold6 and Z Flip6...
spot_imgspot_img

Media queries provide a great way to programmatically change behavior depending on viewing state. We can target styles to device, pixel ratio, screen size, and even print. That said, it’s also nice to have JavaScript events that also allow us to change behavior. Did you know you’re provided events both before and after printing?

I’ve always used @media print in stylesheets to control print display, but JavaScript provides beforeprint and afterprint events:

function toggleImages(hide = false) 
  document.querySelectorAll('img').forEach(img => 
    img.style.display = hide ? 'none' : '';
  );


// Hide images to save toner/ink during printing
window.addEventListener('beforeprint', () => toggleImages(true))
window.addEventListener('afterprint', () => toggleImages());

It may sound weird but considering print is very important, especially when your website is documentation-centric. In my early days of web, I had a client who only “viewed” their website from print-offs. Styling with @media print is usually the best options but these JavaScript events may help!

  • Tips for Starting with Bitcoin and Cryptocurrencies

    One of the most rewarding experiences of my life, both financially and logically, has been buying and managing cryptocurrencies like Bitcoin, Litecoin, Ethereum. Like learning any other new tech, I made rookie mistakes along the way, but learned some best practices along the way. Check out…

  • Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was…OK, I had all of these things too.  But I still don’t get…

  • Duplicate the jQuery Homepage Tooltips Using MooTools

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: Here’s how to accomplish this same effect using MooTools. The XHTML The above XHTML was taken directly from the jQuery homepage — no changes. The CSS The above CSS has been slightly modified to match the CSS rules already…

  • Cross Browser CSS Box Shadows

    Box shadows have been used on the web for quite a while, but they weren’t created with CSS — we needed to utilize some Photoshop game to create them.  For someone with no design talent, a.k.a me, the need to use Photoshop sucked.  Just because we…



Source link

Latest stories

spot_img