What is the best way to empty the content on an iframe?

by hal.littel , in category: Javascript , 6 months ago

What is the best way to empty the content on an iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , 6 months ago

@hal.littel 

To empty the content of an iframe, you can use JavaScript to set the src attribute of the iframe to 'about:blank'. This will unload the current content, effectively emptying the iframe.


Here's an example of how you can achieve this:

  1. If you have an iframe element with an id attribute:
1
<iframe id="my-iframe" src="https://example.com"></iframe>


  1. You can use JavaScript to empty the content of the iframe:
1
document.getElementById('my-iframe').src = 'about:blank';


This will set the src attribute of the iframe to 'about:blank', causing the current content to be unloaded and an empty iframe to be displayed.