JSFiddle - React, Tailwind, and code Playground

by rwaldin

HTML

Before re-adding iframe, set src property to:
<label><input type=radio name='iframe.src' value='undefined' checked>undefined</label>
<label><input type=radio name='iframe.src' value=''>""</label>
<label><input type=radio name='iframe.src' value='about:blank'>"about:blank"</label>
<label><input type=radio name='iframe.src' value='javascript:false'>"javascript:false"</label>

<div><button>Run Test</button></div>

CSS

label{display:block;text-indent:1rem;}

JavaScript

function createNestedIframe(doc, level, srcVal) {
    var iframe = doc.createElement('iframe');

    if (srcVal !== undefined) iframe.src = srcVal;
    doc.body.appendChild(iframe);
    if (!iframe.contentWindow) msg('no contentWindow for iframe added at level ' + level);
    doc.body.removeChild(iframe);

    
    doc.body.appendChild(iframe);
    
    if (!iframe.contentWindow) msg('no contentWindow for iframe re-added at level ' + level);
    else return level++ > 50 || createNestedIframe(iframe.contentDocument, level, srcVal);
}

function msg(msg) {
   document.body.appendChild(document.createElement('p')).innerText = msg;
}  

document.querySelector('button').addEventListener('click', function() {
    var iframe = document.querySelector('iframe'),
        checkedRadio = document.querySelector('input:checked'),
        srcVal = checkedRadio && checkedRadio.value !== 'undefined' ? checkedRadio.value : srcVal;

    if (iframe) {
        while (iframe.nextSibling) iframe.nextSibling.remove();
        iframe.remove();
    }
    if (createNestedIframe(document, 0, srcVal)) msg('Success!');
    msg("srcVal=" + (typeof srcVal == 'string' ? '"' + srcVal + '"' : srcVal));
})