Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any solution to disable Javascript style changes in print?

Is there any solution to disable Javascript style changes in print?

For instance, if I'm hiding something through Javascript but I want to include that hidden info in print.

I hid a div using Javascript and I want to show that div if Javascript is disabled. Now the problem is, because div is hidden using Javascript it's also not showing when the page is printed.

like image 944
Jitendra Vyas Avatar asked Jun 19 '10 12:06

Jitendra Vyas


People also ask

How do I hide the elements when printing a website using CSS?

To hide the element, add “display:none” to the element with CSS.

How do I disable print and Save As in my browser?

There is no direct way to disable print option from browser as it is inbuilt functionality, you may block the keys combination using javascript but may not block print option from file menu.

How do I hide print in HTML?

Use @media print query and set the visibility hidden to that element that needs to hide at printing. Example 1: In this example, hide the element h1 at printing time. To hide the element h1 use media query and set visibility:hidden.


2 Answers

Use a print stylesheet, along with !important statements to force the element to be visible for printing.

<link rel="stylesheet" href="print.css" type="text/css" media="print" />

CSS:

#myDiv { display: block!important; } 
like image 50
Andy E Avatar answered Sep 28 '22 16:09

Andy E


I've found a workaround (at least, it works for me). In my instance i had a basic html page with some styling (screen.css & print.css) plus some javascript to progressively enhance the page with extra features, etc.

When it came time to print the page i realised that the js was affecting the layout (since i was doing some css styling via jquery).

What i ended up doing was this:

in "screen.css"

body {
    background-color: #ccc; /* or whatever colour your designer chose; if it NEEDS to be white, simply change something else (e.g. background-image, font-size, etc.) */
}

in "print.css"

body {
    background-color: #fff;
}

in "the-javascript-file.js"

$(document).ready(function()
{
    if (isPrinting() == false)
    {
        init();
    }
});

function isPrinting()
{
    var isPrint = false;
    /* I'm not 100% sure about the string literal check 'rgb(255, 255, 255)',
       should do some testing here with other values || other attributes...
       (font-size, color, line-height, other attributes that will have the 
       greatest difference / variation between "screen" and "print" styles)
    */
    if ($('body').css('background-color') == 'rgb(255, 255, 255)')
    {
        isPrint = true;
    }
    return isPrint;
}

function init()
{
    // All sorts of awesome goes here
}

And that was it! It worked!

Here's an overview of what's happening:

  • User loads page
  • Browser loads "screen.css"
  • Body background colour is set to "#ccc"
  • Browser loads "the-javascript-file.js"
  • JS checks background colour... it's "#ccc"...
  • JS does its thing
  • User hits print command
  • Browser loads "print.css"
  • Body background colour changes to "#fff"
  • Browser loads "the-javascript-file.js"
  • JS checks body background colour
  • JS realises background colour is "#fff"
  • JS does nothing :)

Hope this helps someone out there :)

like image 41
barryels Avatar answered Sep 28 '22 15:09

barryels