2015年2月

来段js

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
    <iframe data="<p>test</p>" id="iframe"></iframe>
</body>
<script type="text/javascript">
    var iframe = document.getElementById('iframe');
    var html = iframe.getAttribute("data");
    var iwindow = iframe.contentWindow;
    var idocment = iwindow.document;

    // iwindow.onload = function() {
    //     ihead = idocment.head || idocment.getElementsByTagName('head')[0];
    //     ibody = idocment.body || idocment.getElementsByTagName('body')[0];
    //     var div=idocment.createElement("div");
    //     div.innerHTML = html;
    //     ibody.appendChild(div);
    // };

    var ihead = idocment.head;
    var ibody = idocment.body;
    if (!ihead) {
        iwindow.onload = function(e) {
            ihead = idocment.getElementsByTagName('head')[0];
            ibody = idocment.getElementsByTagName('body')[0];
            var div=idocment.createElement("div");
            div.innerHTML = html;
            ibody.appendChild(div);
        };
    } else {
        idocment.open();
        idocment.writeln(
            '<html><head><head><body>' + html + '<body></html>'
        );
        idocment.close();
//         var div=idocment.createElement("div");
//         div.innerHTML = html;
//         ibody.appendChild(div);
    }
</script>
</html>