August 17, 2024
JavaScript Print
JavaScript's print function is one of the most important functions among all the functions. This function use to print all the contents of a webpage on a paper. Print function sends current webpage's all the contents to the printer. Usually it works as same as browser's print command works which presents in file menu. But sometimes it happened that you want to provide print functionality through your code as per special requirement so in that case you can use print function of JavaScript. You can call print function on any event such as onClick event, onKeyDown etc. The example of print function mentioned below:
Code
<html>
<head>
<title>Print Function Example</title>
</head>
<body>
<p>Hello, welcome in the world of JavaScript programming.</p>
<p>JavaScript is very easy to learn.</p>
<form name='myForm' method='post'>
<input type='button' onclick='window.print();' name='btn1'
value='Print Webpage' />
</form>
</body>
</html>
In above mentioned example we have created a webpage which has two paragraphs and a button which called print function of JavaScript. We call print function with window object because it is a function of window object, so whenever we want to call it we should write window.print() statement. Now whenever user will click on 'Print Webpage' button then it will sent webpage's content to the printer and print it.