How to Add Code to an iFrame Using a Blob

Sometimes, you may need to dynamically add code to an iframe. This can be achieved using Blobs in JavaScript.

// Create a new Blob with HTML content
var htmlContent = '<body>Hello, world!</body>';
var blob = new Blob([htmlContent], { type: 'text/html' });

// Create an iframe and append to body
var iframe = document.createElement('iframe');

// Write the blob content to the iframe
iframe.src = URL.createObjectURL(blob);

This code snippet creates a new Blob object with some HTML content, creates an iframe, and then uses the URL.createObjectURL() method to create a URL representing the Blob object, and assigns it to the iframe's src attribute.

Copyright © 2024. Design and code by myself with Next.js. Fork it and create yours