Is there anyway to automatically run javascript:window.print() when the page finishes loading?
Is there anyway to automatically run javascript:window.print() when the page finishes loading?
Add the following code in your HTML page, and it will show print preview on page load.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function () {
window.print();
});
</script>
If what you want is to open a separate window from the web browser you can use this:
window.open(basePath + "Controller/Route/?ID=" + param, '_blank').print();
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.