Specifying content of an iframe instead of the src attribute to a page

后端 未结 4 557
灰色年华
灰色年华 2020-11-29 00:06

I\'m currently working on a form which includes some file inputs to upload pictures. There is an onchange() event for those inputs that submits the pictures to

相关标签:
4条回答
  • 2020-11-29 00:39

    You can .write() the content into the iframe document. Example:

    <iframe id="FileFrame" src="about:blank"></iframe>
    
    <script type="text/javascript">
       var doc = document.getElementById('FileFrame').contentWindow.document;
       doc.open();
       doc.write('<html><head><title></title></head><body>Hello world.</body></html>');
       doc.close();
    </script>
    
    0 讨论(0)
  • 2020-11-29 00:40

    In combination with what Guffa described, you could use the technique described in Explanation of <script type = "text/template"> ... </script> to store the HTML document in a special script element (see the link for an explanation on how this works). That's a lot easier than storing the HTML document in a string.

    0 讨论(0)
  • 2020-11-29 00:43

    iframe now supports srcdoc which can be used to specify the HTML content of the page to show in the inline frame.

    0 讨论(0)
  • 2020-11-29 00:46

    You can use data: URL in the src:

    var html = 'Hello from <img src="http://stackoverflow.com/favicon.ico" alt="SO">';
    var iframe = document.querySelector('iframe');
    iframe.src = 'data:text/html,' + encodeURIComponent(html);
    <iframe></iframe>

    Difference between srcdoc=“…” and src=“data:text/html,…” in an iframe.

    Convert HTML to data:text/html link using JavaScript.

    0 讨论(0)
提交回复
热议问题