JavaScript: Print the contents of the current window
JavaScript Basic: Exercise-2 with Solution
Write a JavaScript program to print the current window contents.
This JavaScript program defines a function that uses the window.print() method to print the contents of the current browser window. When the function is called, it triggers the print dialog, allowing the user to print the current webpage.
Sample Solution:
JavaScript Code:
// Define a function named print_current_page
function print_current_page() {
// Call the window.print() method to initiate the printing of the current page
window.print();
}
Output:
Click the button to print the current page.
Live Demo:
See the Pen JavaScript current day and time - basic-ex-2 by w3resource (@w3resource) on CodePen.
Explanation:
window.print(): The window object represents a window containing a DOM document; the document property points to the DOM document loaded in that window, window.print() is used to open the Print Dialog to print the current document.
ES6 Version:
function print_current_page()
{
window.print();
}
Improve this sample solution and post your code through Disqus.
Previous: JavaScript program to display the current day and time in a specific format.
Next: JavaScript program to get the current date.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/javascript-exercises/javascript-basic-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics