13

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>
5
  • 3
    No, such thing is not possible by design. What is the point of <iframe> if all you want is to have it display static content? Just use <div> tag for this. Commented Mar 21, 2012 at 12:53
  • see my answer under the other comment! thanks Commented Mar 21, 2012 at 12:55
  • What do you mean by "don't have to care about referring & stuff"? Commented Mar 21, 2012 at 13:03
  • the problem is that the code I insert somehow referrs my whole page so the code is not displayed INSIDE the div but as a new page.. Commented Mar 21, 2012 at 13:09
  • Sorry, no idea what you mean. Anyway my first comment stands - you can't put contents inside <iframe> that's simply not how it should be used. Commented Mar 21, 2012 at 13:17

4 Answers 4

14

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

It's currently not supported by any browser.
2

srcdoc attribute of iframe is the simple method...

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

Comments

0

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

2 Comments

Thanks for your reply. Actually that was my previous approach but as I wrote in this post stackoverflow.com/questions/9803700/… it doesn't work in this particular case. That is why I wanted to use a frame, so I don't have to care about referring & stuff as all happens inside that frame.
I did not know the srcdoc attribute, thanks ! But i suspects that it isn't supported for the majority of browsers. I found this information in that page: html-5.com/tags/iframe-tag/index.html#attributes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.