How to make a div, an iframe and write inside the iframe with javascript

Tagged:  

What this script does is it makes a div, it attaches it to the dom, in puts an iframe inside the div, and then it writes things inside the iframe.
<html><head></head><body>
aaa
<script>
var s2div = document.createElement('div');
s2div.setAttribute('id', 's2info');
s2div.height = '0px';
s2div.width = '0px';
s2div.style.display = 'none';
s2div.innerHTML = '<iframe  width="100px" height="100px" style="display:none" id = "idIframe"></iframe>';
 
var theBody = document.getElementsByTagName('body')[0];
theBody.appendChild(s2div);

val = 'bbbbb';

  var testFrame = document.getElementById("idIframe");
        var doc = testFrame.contentDocument;
        if (doc == undefined || doc == null)
            doc = testFrame.contentWindow.document;
        doc.open();
        doc.write(val);
        doc.close();
        s2div.style.display = 'block';
    testFrame.style.display = 'block';
 </script>
</body>
</html>