Three Ways to Create HTML Documents on the Fly

Creating HTML documents on the fly, with or without JavaScript, is sometimes necessary. Whether the goal is to display an acknowledgment page or an iframe containing a whole page, if the document is simple enough it can be easily put together and served with either data URLs or JavaScript.

But, how do you go about it? I’m sure you already know how to add HTML to a document using JavaScript, but to create a whole HTML document? You might be interested in some of the methods I’ll be showing below, the first of which doesn’t even need JavaScript!

I’ll show all the newly created documents in iframes so that you could see them rendered. However, you can use the documents however you see fit. For instance, they can be saved into a database or sent across web services to be rendered somewhere else.

1. Data URLs

Data URLs provide you with a simple and effective method to serve documents on a web page. If you aren’t familiar with it read our previous article on how they work.

Basically, data URLs start with the data: URL scheme. It’s followed by the content to be served, before which you can optionally mention the media type and the encoding of the content.

You might’ve seen images served this way, where base64 characters are given as the content of the data URL, following a media type.

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAC/VBMVEUAAADphwDqhwDqiAD7kADqiAABi8nqiADP2N0AitnqiADriAD5wTXQ2t3ohwDvmxMBi8nqiADriADohgDlhgDlhQDU2Nj1vCzP2N31vCznhgABi8n2vCzqiAACi8n0vSz2vCvP2N7riADQ2d3qiQAAi8kAjMrvvjDzmhzqiAD1vCzP2d3Q2N31vCzO2N31vCzP2N3mhwDO2N7Q2N3wryAAiszOzufP2N39ykfP2N31vCyPwNf2vCzriAD+y0cBi8n1vCzqiADP2N3P2Nz2vjDqiADriQD2vSvO2N0Aisn0uyvQ2d70vCvriQDkhQDQ2N4AjMnqiAD1vCzbfwDtmA/tmQ0Bi8rP2d0Ci8kCisn9yUYAi8j9ykYAiskAisrpiAAAjcrP2N39ykfqiAC+w8/m6+8Bi8n1vCxFMyzWfAD19vaZqK7mpAD////R2t/z9ffx9PWptLynsrrpqAb7xT/8yEVINi7f5unT3OCrtb7jhADonyD7/Pz3+frq7vD8yEL5wzrsrhHaggTYfQDk6u7v8vTc4+fZ4eX3xUb4wTX3vzDzuSjrqgvcgAD28+qstr/vsxzwoBjtsBbY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hxyPoJ2m3EQKj0updUbhPx/glistrgJB2kfudQD8naCGpQpLyn2+jz0BjzLFEqNrapCiXhZcShXlghKlW1tKT9qgLvzz43R/wAAAABJRU5ErkJggg=="
alt="man with a computer">

The above code displays a PNG image of the man with a laptop emoji—you can check it in your browser.

Data URL example with Base64 images

Similar to how this is done, data URLs can also serve HTML documents:

<iframe src='data:text/html,<strong>Hello World!</strong>'></iframe>

The iframe renders the HTML document that was added using the data URL containing the text/html media type & followed by the HTML code.

You can edit the Codepen demo below by adding extra HTML to it if you want to see how the technique works.

2. DOMImplementation interface

DOMImplementation is an interface that can create brand new documents using either its createDocument() (for XML) or createHTMLDocument() method—whichever you need. The interface is accessed using the document.implementation object.

The createHTMLDocument() method takes one optional parameter which is the title of the new document.

You can add HTML to a newly created document the same way you usually do: by using methods such as append(), appendChild(), and other DOM-related JavaScript methods.

<iframe src=""></iframe>
window.onload = () => {
  var doc = document.implementation.createHTMLDocument();
  doc.body.append('Hello World!');
  var iframeDoc = document.querySelector('iframe').contentDocument;
  iframeDoc.replaceChild(
  doc.documentElement,
  iframeDoc.documentElement
  );
}

In the code above, a new HTML document is created using the createHTMLDocument() method of the DOMImplementation interface and the Hello World! string is added to its body element.

Then, to see how the newly created document looks when it’s rendered, I replaced the document element of the iframe (iframeDoc.documentElement) with the document element of the new document (doc.documentElement) using the replaceChild() method. This way, you’ll be able to see the Hello World! string from the document we created and added to the iframe.

3. DOMParser API

As its name suggests, the DOMParser API parses HTML/XML strings into DOM documents.

A new DOMParser object instance can be created using its constructor, DOMParser(). The instance holds a method called parseFromString() that does the parsing after taking two arguments: the string to be parsed and the document type of the document to be created.

<iframe src=""></iframe>
window.onload = () => {
  var parser = new DOMParser();
  var doc = parser.parseFromString('<strong>Hello World! </strong>', "text/html");
  doc.body.append('extra text');
  var iframeDoc = document.querySelector('iframe').contentDocument;
  iframeDoc.replaceChild(doc.documentElement, iframeDoc.documentElement);
}

In the above code, a new DOMParser instance parses a HTML string to a DOM document using the parseFromString() method.

Then, in the same way as in the previous code snippet, the document element of the newly created document replaces the document element of the iframe. As a result, the HTML code in the document we created becomes visible in the iframe.

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail