How to print the content of current window using JavaScript ? (original) (raw)
Last Updated : 26 Jul, 2024
The task is to print the content of the current window by using the **window.print() method in the document. It is used to open the Print Dialog Box to print the current document. It does not have any parameter value.
**Syntax:
window.print()
**Parameters: No parameters are required
**Returns: This function does not return anything
**Example: This example representing how to print the content of current window using JavaScript.
html `
GeeksForGeeks - Print PageHello GeeksForGeeks Users
Print the content of the current window using HTML and JavaScript
This is a sample paragraph that will be included in the printed page. You can add more content here as needed.
<script>
function printCurrentPage() {
window.print();
}
// Add a listener for the beforeprint event
window.addEventListener('beforeprint', function() {
console.log('Printing started');
});
// Add a listener for the afterprint event
window.addEventListener('afterprint', function() {
console.log('Printing finished');
});
</script>
`
**Output:
https://media.geeksforgeeks.org/wp-content/uploads/20240725220311/Print.mp4
Similar Reads
- Print the content of a div element using JavaScript If you want to print the content of a element using JavaScript, you can achieve this by extracting the content and then opening a new window or popup to display and print it. This method involves capturing the content of the, creating a new window, and using the print command 2 min read
- How to get the child element of a parent using JavaScript ? Given an HTML document, the task is to select a particular element and get all the child elements of the parent element with the help of JavaScript. Below are the approaches to get the child element of a parent using JavaScript: Table of Content By using the children propertyBy using the querySelect 3 min read
- How to close current tab in a browser window using JavaScript? In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo make this, we will use window.close() method.  This method is used to close the window which is opened by the window.open() method. Syntax:window.close();But accounting for a secu 1 min read
- How to get the Width of Scroll bar using JavaScript? Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth 3 min read
- How to list all the cookies of the current page using JavaScript ? Cookies are small pieces of data stored by websites on your browser to remember information about your visit, such as login status, preferences, or other settings. In JavaScript, you can easily retrieve cookies stored by the current domain using document.cookie. However, due to security reasons, you 3 min read
- How to calculate Width and Height of the Window using JavaScript ? In this article, we will know to calculate the width & the height of the window using Javascript. Given an HTML document that is running on a Window & we need to find the height and width of the window. The Window innerWidth property is used for returning the width of a window’s content area 2 min read
- How to Print a Message to the Error Console Using JavaScript ? This article will show you how to print a message using JavaScript on the error console. There are three methods to display the error console, these are: Table of Content Using console.error() MethodUsing console.warn() MethodUsing console.info() MethodApproach 1: Using console.error() MethodThe con 2 min read
- How to Get the Content of an HTML Comment using JavaScript ? In this article, we will learn how to get the content of an HTML Comment Using Javascript. Comments are a best practice in programming and software development. In general, they can explain why a coding decision was made or what needs to be done to improve the code you're working on. HTML tags (incl 3 min read
- How to Get the Current URL using JavaScript? Here are two different methods to get the current URL in JavaScript. 1. Using Document.URL PropertyThe DOM URL property in HTML is used to return a string that contains the complete URL of the current document. The string also includes the HTTP protocol such as ( http://). Syntax document.URLReturn 1 min read
- How to print the content of an object in JavaScript ? To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: C/C++ Code // Given Object const obj = { name: 'John', age: 30, city: 3 min read