Skip to content

Instantly share code, notes, and snippets.

@ryan-ethode
Created March 3, 2016 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryan-ethode/62a28a957414b3ac0da0 to your computer and use it in GitHub Desktop.
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.
<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