Created
March 3, 2016 16:58
-
-
Save ryan-ethode/62a28a957414b3ac0da0 to your computer and use it in GitHub Desktop.
A print button that only prints the contents of a specific div, rather than a whole page.
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
<div id="print-contents"> | |
<!-- Your content goes here. --> | |
</div> | |
<script> | |
printDivCSS = new String ('<link href="../css/main.css" rel="stylesheet" type="text/css">') | |
//The line referencing your CSS will keep your printed div formatting looking correct. | |
function printDiv(divId) { | |
window.frames["print_frame"].document.body.innerHTML=printDivCSS + document.getElementById(divId).innerHTML; | |
window.frames["print_frame"].window.focus(); | |
window.frames["print_frame"].window.print(); | |
} | |
</script> | |
<iframe name="print_frame" width="0" height="0" frameborder="0" src="about:blank"></iframe> | |
<a class="btn btn-default" href="javascript:printDiv('print-contents')">Print</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment