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 tutorial by Intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked May 14, 2021 in Java by sheela_singh (9.5k points)

Browse Categories

...