Created
February 3, 2015 08:23
-
-
Save iansltx/2905c9336e369fb7e6c1 to your computer and use it in GitHub Desktop.
Force element re-render on print
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { // force re-render of element_name to get the styling right on-print | |
var beforePrint = function() { | |
var el = document.getElementById('element_name'); // e.g. an input inside a <p>... | |
var p = el.parentNode; // ...which will normally fail to style entirely on-print... | |
p.removeChild(el); // ...but if you wrap it in a <span> or the like, then remove and re-add the element... | |
p.appendChild(el); // ...the styles will recalculate and you'll get whatever style you were trying to apply! | |
}; | |
if (window.matchMedia) { | |
window.matchMedia('print').addListener(function(mql) { | |
if (mql.matches) beforePrint(); | |
}); | |
} | |
window.onbeforeprint = beforePrint; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment