HTML <frame> SRC-attribute - use html-code instead of URL

前端 未结 4 887
Happy的楠姐
Happy的楠姐 2021-02-05 16:32

Is there a way to use pure html-code to display inside a frame instead of having to link to a specific URL/file?

For example:

NOT like this

相关标签:
4条回答
  • 2021-02-05 17:07

    maybe you could inject HTML into the iFrame/Frame like described in this article:Injecting HTML into an IFrame by Michael Mahemoff.

    Something like this:

    var content = "<html><body><b>Hello World!</b></body></html>";
    
    var iframe = document.createElement("iframe");
        document.body.appendChild(iframe);
    
    var frameDoc = iframe.document;
        if(iframe.contentWindow)
            frameDoc = iframe.contentWindow.document; // IE
        // Write into iframe
        frameDoc.open();
        frameDoc.writeln(content);
        frameDoc.close();
    

    HTH,

    --hennson

    0 讨论(0)
  • 2021-02-05 17:07

    I think that you cannot do this. Maybe you can try with a DIV an modify the content with javascript with innerHTML?

    <div id='A' onclick="javascript:document.getElementById('A').innerHTML = 'Another content'">
        Click me
    <div>
    ​
    
    0 讨论(0)
  • 2021-02-05 17:11

    srcdoc attribute of iframe is the simple method...

    <iframe srcdoc="Your text goes here..."></iframe>

    0 讨论(0)
  • 2021-02-05 17:21

    In HTML5 there appears to be a way to do this:

    <iframe seamless sandbox srcdoc="<p>did you get a cover picture yet?"></iframe>

    See here, supposedly this is the purpose of the new html5 srcdoc attribute.

    According to the MDN, it appears only chrome will honor the srcdoc attribute at this time.

    I wasn't able to get this attribute to work, so it's probably not a viable option at this time. As others suggested, using a div is probably a better solution.

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