I have discovered a solution that has proven effective for me. In my specific case, I had a simple html page with some styling (screen.css & print.css) and javascript to enhance the page with additional features.
When it was time to print the page, I noticed that the javascript was impacting the layout (as I was applying css styling through jquery).
Here's what I did:
in "screen.css"
body {
background-color: #ccc; /* or any other color chosen by your designer; if it needs to be white, simply change another attribute (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;
if ($('body').css('background-color') == 'rgb(255, 255, 255)')
{
isPrint = true;
}
return isPrint;
}
function init()
{
// Awesome stuff happens here
}
And that's it! It worked!
Here's a summary of the process:
- User opens the page
- Browser loads "screen.css"
- Body background color is set to "#ccc"
- Browser loads "the-javascript-file.js"
- Javascript checks background color... it's "#ccc"...
- Javascript performs its tasks
- User initiates the print command
- Browser loads "print.css"
- Body background color changes to "#fff"
- Browser loads "the-javascript-file.js"
- Javascript checks body background color
- Javascript realizes background color is "#fff"
- Javascript does nothing :)
Hopefully, this solution proves helpful to someone out there :)