JavaScript

Remove Iframe html using Javascript

Let”s say that you are showing an iframe in a page. If you notice, you will see that the contents of the page will also be shown in the iframe by default. Using this simple 1 line trick, you can remove iframe html using javascript.

How To Remove Iframe html using Javascript Example

Let’s assume that we have an IFrame with ID “myframe”. We can use the .setAttribute() method to completely remove the html contents from the Iframe.

Example: Remove Iframe html using Javascript

//HTML:

<iframe id="myframe" src="https://www.google.com"></iframe>

//Javascript:

document.getElementById( 'myframe' ).setAttribute( 'src', '' );

When you use the above code in your own web page and run it, you will see the contents of the iframe cease to exist. Easy, eh?

Your Turn!

Do you know of any other ways of using JavaScript to remove iframe html? Feel free to share by commenting below.

One Comment on Remove Iframe html using Javascript

  1. 1

    Awesome. thanx man was really helpful..

Share your thoughts, comment below now!

*

*