EAzevedo
EAzevedo

Reputation: 791

Dynamically create an HTML document using javascript using window

I need to create a new HTML page using javascript. To do that I tried using the window object

let myDocument = window;

myDocument.document.write(`
     <html>
         <head>
               <title>${title}</title>
         </head>
         <body><section id="print"></section></body>
     </html>
`)

My problem is that I would like to generate that on the background and save the result somewhere in my project.

These are the two problems I have at the moment. I can generate the page, but I could not find out how to do that on the background. My limitation is that the project uses pure javascript so Node.js packages might not work.

Upvotes: 1

Views: 297

Answers (1)

Yogendra Chauhan
Yogendra Chauhan

Reputation: 825

Instead of document.write you can save it in a variable & use it when you want or save it through service or localStorage.

You code must be like:

const title = "Hello World!";
let myDocument =
     `<html>
         <head>
               <title>${title}</title>
         </head>
         <body><section id="print"></section></body>
     </html>`;
     
console.log(myDocument);

Upvotes: 1

Related Questions