Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code which has html/xml as a JavaScript object:

var name = "Matt";

var htmlDocumentToPrint = "<html><body><div style='width:300px; height:20px; background-color:#000; text-align:center;'>My name is " + name + "</div></body></html>";

htmlDocumentToPrint.print();

Though window.print() can print the current page, can anyone tell me if I can  build a document using JavaScript in order to populate it with data and print it off?

1 Answer

0 votes
by (19.7k points)

To create a document in a window using JavaScript then to call print() on the window object, you can do something like below: 

<script type="text/javascript">

    var myWindow = window.open('','','width=200,height=100')

    myWindow.document.write("This is 'myWindow'")

    myWindow.print();

</script>


 

Interested in Java? Check out this Java Certification by Intellipaat. 

Browse Categories

...