iframe

Summary

The HTML inline frame element (<iframe>) represents a nested browsing context, effectively embedding another HTML page into the current page. Each browsing context has its own session history and active document. The browsing context that contains the embedded content is called the parent browsing context. The top-level browsing context (which has no parent) is typically the browser window.

Usage context

Permitted content Normal character data (text and character references)
Tag omission None, must have both a start tag and an end tag.
Permitted parent elements Any element that accepts phrasing content.
Normative document HTML 5, section 4.8.2; HTML 4.01, section 16.5

Attributes

Like all HTML elements, this element supports the global attributes.

align Deprecated in HTML 4.01 , Obsolete in HTML5
The alignment of this element with respect to the surrounding context.
frameborder HTML 4  only
The value 1 (the default) tells the browser to draw a border between this frame and every other frame. The value 0 tells the browser not to draw a border between this frame and other frames.
height
Indicates the height of the frame HTML5 in CSS pixels, or HTML 4.01 in pixels or as a percentage.
longdesc HTML 4  only
A URI of a long description of the frame. Due to widespread misuse, this is not helpful for non-visual browsers.
marginheight HTML 4  only
The amount of space in pixels between the frame's content and its top and bottom margins.
marginwidth HTML 4  only
The amount of space in pixels between the frame's content and its left and right margins.
mozallowfullscreen Non-standard
In Gecko 9.0 or later, this attribute can be set to true if the frame is allowed to be placed into full screen mode by calling its element.mozRequestFullScreen() method. If this isn't set, the element can't be placed into full screen mode.
webkitallowfullscreen Non-standard
In Chrome 17 or later (and maybe earlier), this attribute can be set to true if the frame is allowed to be placed into full screen mode by calling its element.webkitRequestFullScreen() method. If this isn't set, the element can't be placed into full screen mode.
name
A name for the embedded browsing context (or frame). This can be used as the value of the target attribute of an <a> or <form> element, or the formtarget attribute of an <input> or <button> element.
scrolling HTML 4  only
Enumerated attribute indicating when the browser should provide a scroll bar (or other scrolling device) for the frame:
  • auto: Only when needed.
  • yes: Always provide a scroll bar.
  • no: Never provide a scoll bar.
sandbox HTML5 only
If specified as an empty string, this attribute enables extra restrictions on the content that can appear in the inline frame. The value of the attribute can be a space-separated list of tokens that lift particular restrictions. Valid tokens are:
  • allow-same-origin: Allows the content to be treated as being from the same origin as the containing document. If this keyword is not used, the embedded content is treated as being from a unique origin.
  • allow-top-navigation: Allows the embedded browsing context to navigate (load) content from the top-level browsing context. If this keyword is not used, this operation is not allowed.
  • allow-forms: Allows the embedded browsing context to submit forms. If this keyword is not used, this operation is not allowed.
  • allow-scripts: Allows the embedded browsing context to run scripts (but not create pop-up windows). If this keyword is not used, this operation is not allowed.
seamless HTML5  only
This Boolean attribute indicates that the browser should render the inline frame in a way that makes it appear to be part of the containing document, for example by applying CSS styles that apply to the <iframe> to the contained document before styles specified in that document, and by opening links in the contained documents in the parent browsing context (unless another setting prevents this).
src
The URL of the page to embed.
srcdoc HTML5 only
The content of the page that the embedded context is to contain.
width
Indicates the width of the frame HTML5 in CSS pixels, or HTML 4.01 in pixels or as a percentage.

DOM interface

Scripting

Inline frames, like <frame>  elements, enter the window.frames pseudo-array.

From the DOM iframe element, scripts can get access to the window object of the included HTML page via the contentWindow property. The contentDocument property refers to the document element inside the iframe (this is equivalent to contentWindow.document), but is not supported by Internet Explorer versions before IE8.

From the inside of a frame, a script can get a reference to the parent window via window.parent.

Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain. This also applies to a script inside a frame trying to access its parent window. Cross-domain communication can still be achieved with window.postMessage.

Example

<script type="text/javascript">
  var iframe = window.getElementsByTagName( "iframe" )[ 0 ];
  alert( "Frame title: " + iframe.contentWindow.title );
</script>


<iframe src="page.html" width="300" height="300">
  <p>Your browser does not support iframes.</p>
</iframe>

 Notes

Gecko 6.0 note
(Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3)

Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3) , rendering of inline frames correctly respects the borders of their containing element when they're rounded using border-radius .

Browser compatibility

  • Desktop
  • Mobile

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 (Yes) (Yes) (Yes) (Yes)
sandbox 4 -- ? ? ?
seamless 4 -- ? ? ?
srcdoc 4 -- ? ? ?
mozallowfullscreen -- 9.0 (9.0) -- -- --
webkitallowfullscreen 17 (maybe 15+?) -- -- -- --
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
sandbox ? -- ? ? ?
seamless ? -- ? ? ?
srcdoc ? -- ? ? ?
mozallowfullscreen -- 9.0 (9.0) -- -- --

Tags (5)

Edit tags

Attachments (0)

 

Attach file