2

I'm not that familiar with iframes and basically just want to find out if it's possible to exclude the 'src' attribute on an iframe tag and instead embed a html tag inside of it? For example:

<iframe id='myFrame'>
    <html>
        <head>...</head>
        <body>
            <h3>Hello World!</h3>
        </body>
    </html>
</iframe>

Also just a little side question, say this is indeed possible. And you're embeding this code in a JSP, would the java variables in the JSP also be visible to the html document in side the iframe's html tag?

Thanks guys!

  • 1
    you can read the answer to this post http://stackoverflow.com/questions/620881/putting-html-inside-an-iframe-using-javascript – Jayyrus May 14 '12 at 07:08

3 Answers3

3

I don't know why you would want to do something like that since it would make more sense to use a div, place what you want in the div and use javascript to display the div when needed. Typically the reason to put only text, not html tags, in an iframe without the source attribute would be to support browsers that don't support iframes.

The purpose of an iframe is to display a different web page within the iframe and it seems like what your basically trying to do is create another web page within the iframe without using the src attribute, which is wierd, and since you're using jsp it would make more sense to create the page and use a

<jsp:include>

to include the page instead of using an iframe.

ChadNC
  • 2,528
  • 4
  • 25
  • 39
  • One "why to do this" could be attempt to inject dynamically modified content, retrieved by using some kind of proxy script _(for example using php socket functions)_, inside iframe. This way it is possible to modify contents of _cross-site_ ` – Sampo Sarrala - codidact.org Jan 07 '13 at 10:32
1

Maybe you can use a DIV instead of your iframe and innerhtml what you want... it would be the same no?

mlwacosmos
  • 4,391
  • 16
  • 66
  • 114
0

I know this is a super old question but I just came across it and there have been new developments since 2010.

This is now possible using the HTML5 srcdoc attribute. From MDN:

The content of the page that the embedded context is to contain. This attribute is expected to generally be used together with the sandbox attribute. If a browser supports the srcdoc attribute, it will override the content specified in the src attribute (if present). If a browser does NOT support the srcdoc attribute, it will show the file specified in the src attribute instead (if present). Note that if the content of the attribute contains a script tag then a closing script tag is required for the script to run, even if nothing else comes after the script.

Note, however, that srcdoc is not supported by any version of Internet Explorer. Edge has you covered, though.

If you're interested in a polyfill, there is one available but I have not (yet!) tested it out for myself:

https://github.com/jugglinmike/srcdoc-polyfill

dgrundel
  • 568
  • 3
  • 7