faqts : Computers : Programming : Languages : JavaScript : Frames

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

43 of 48 people (90%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I dynamically delete an iframe?

May 7th, 2000 06:49
Martin Honnen,


You can do so in IE4/5 by setting the outerHTML to '' and in NN6 (and 
IE5) by using dom methods:

<HTML>
<HEAD>
<SCRIPT>
var i = 0;
function deleteIframe () {
  i--;
  if (document.all)
    document.all['iframe' + i].outerHTML = '';
  else if (document.getElementById) {
    var ifr = document.getElementById('iframe' + i);
    ifr.parentNode.removeChild(ifr);
  }
}
function addIframe () {
  if (document.all)
    document.body.insertAdjacentHTML('beforeEnd',
      '<IFRAME SRC="http://JavaScript.FAQTs.com"'
      + ' NAME="iframe' + i + '"'
      + ' IFRAME="iframe' + i++ + '"><\/IFRAME>');
  else if (document.getElementById) {
    var ifr = document.createElement('IFRAME');
    ifr.src = 'http://JavaScript.FAQTs.com';
    ifr.name = 'iframe' + i;
    ifr.id = 'iframe' + i++;
    document.body.appendChild(ifr);
  }
}
</SCRIPT>
</HEAD>
<BODY>
<BUTTON ONCLICK="addIframe()" STYLE="color: white; background-color: 
orange">
add JavaScript.FAQTs.com
</BUTTON>
<BUTTON ONCLICK="deleteIframe()" STYLE="color: white; background-color: 
orange">
delete last inserted iframe
</BUTTON>
<BR>
</BODY>
</HTML>