JavaScript Example: Print This Page Unobtrusive
x
<title>JavaScript Example: Print This Page Unobtrusive</title>
<script>
// Wait for DOM to load
document.addEventListener("DOMContentLoaded", function(event) {
// Put the button into a variable
var e = document.getElementById("print");
// Wait for user to click the button
e.addEventListener("click", function() {
window.print();
}, false);
});
</script>
<!-- Replace '{action page}' with your own action page to support non-JavaScript users -->
<form name="myForm" action="{action page}">
<input id="print" type="button" value="Print this Page">
</form>
In this example, the left side provides the code used to render the output in the right side.
Feel free to copy and paste the code into your own project and modify as you wish.
About JavaScript
JavaScript is a scripting language that enables us to add functionality to web pages. We can combine JavaScript with our HTML code to achieve this outcome. We typically enclose our JavaScript inside <script>
tags, but we can also use event handlers (such as onclick
, onchange
, onmouseover
etc) within the HTML tags themselves to trigger the JavaScript.