JavaScript DOM Print

JavaScript Styled Print Dialog

Create DOM Print In JavaScript

To demonstrate, DOM Print with JavaScript, a new window will be opened in a new web browser window or tab.

Multiple windows can be opened and reused assuming that JavaScript is not disabled. JavaScript can be used to create HTML elements, attributes and styles dynamically without reloading the page.

JavaScript open() method can take optional parameters for the name, URL and window dimensions. Built-in methods can be used to interact with HTML elements via ID, class or tag name.

Common Syntax Of DOM Print In JavaScript

Glossary:

URL

Page to open.

DOM

Document Object Model.

HTML

Hypertext Markup Language.

CSS

Cascading Style Sheets inherit properties and methods from the parent.

Print

Print dialog for current page, allows user to print its contents.

Common Methods For DOM Print

Ojamboshop.com Learning JavaScript DOM Print
Name Description Example
open() Create a new browser window. window.open();
createElement() Create a new HTML element. createElement(‘div’);
querySelector() Retrieve the first element that matches a specified CSS selector. querySelector(‘div’);
querySelectorAll() Retrieve all elements that matches a specified CSS selector. querySelectorAll(‘div’);
appendChild() Add a new child note to the end of specified parent node. appendChild(elem);
print() Opens print dialag to print current document. print();
cloneNode() Clone an element with optional true or false argument for cloning child nodes. cloneNode(true);
Name Description Example

JavaScript DOM Print Snippet

// OjamboShop.com Learning JavaScript DOM Print Tutorial

// DOM HTML
let newWindow = window.open("", null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");  

// New Window Content
let newWindow = window.open("", null, "height=400,width=800,status=yes,toolbar=no,menubar=no,location=no");
let title = "<title>Shopping Invoice</title>";
let style = document.querySelector('style').cloneNode(true);
let printContent = document.querySelector('table').cloneNode(true);
let div = newWindow.document.createElement("div");
div.appendChild(printContent);
newWindow.document.body.appendChild(div);
newWindow.document.head.innerHTML = title;
newWindow.document.head.appendChild(style);
newWindow.print();

JavaScript DOM Print Button
Web Browser Display Print Button

JavaScript DOM Print Result
Web Browser Displaying Print Dialog


Usage

You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE can used to input and compile JavaScript code for the open window DOM HTML.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. The permissive license requires the preservation of the copyright notice and disclaimer. It allows commercial use, modification, distribution, and allows making derivatives proprietary, consult the license for more specific details.

Conclusion:

JavaScript makes it easy to create DOM print content for new windows. New windows can be opened in the same window, as a new window or new tab.

If you enjoy this article, consider supporting me by purchasing one of my OjamboShop.com Online Programming Courses or publications at Edward Ojambo Programming Books or simply donate here Ojambo.com Donate

References:

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP and Mastering Blender Python API, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📖 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.