Is there anyway to automatically run javascript:window.print() when the page finishes loading?
6 Answers
<body onload="window.print()">
or
window.onload = function() { window.print(); }
4 Comments
window.onload = window.print;window.onload = window.print.bind(); ;)The following code must be put at the end of your HTML file so that once the content has loaded, the script will be executed and the window will print.
<script type="text/javascript">
<!--
window.print();
//-->
</script>
For me, adding <script>window.print();</script> to the end of the page worked.
I didn't need the type="text/javascript" attribute, or even for the page to be wrapped in a <body> tag. However, all of my previous attempts to intuitively use the answers suggested here, of just writing window.onload=window.print or longer versions such as window.onload=()=>window.print(); did not work, and of course calling print on the newly created window does not wait for the contents to load.