HTML <frame> SRC-attribute - use html-code i

2019-03-25 06:59发布

问题:

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

<iframe src="left.html" name="left"></iframe>

but like this

<iframe src="here goes the html code" name="thank you SO"></iframe>

回答1:

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



回答2:

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.



回答3:

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>
​


回答4:

srcdoc attribute of iframe is the simple method...

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



标签: html iframe