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

◇◆丶佛笑我妖孽 提交于 2019-12-20 13:04:22

问题


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>


来源:https://stackoverflow.com/questions/9804739/html-frame-src-attribute-use-html-code-instead-of-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!