Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Quirksmode had a <a href="http://web.archive.org/web/20080225210716/http://www.quirksmode.org/js/iframe.html" rel="noreferrer">post on this</a>.</p> <p>Since the page is now broken, and only accessible via archive.org, I reproduced it here:</p> <p><strong>IFrames</strong></p> <p>On this page I give a short overview of accessing iframes from the page they’re on. Not surprisingly, there are some browser considerations.</p> <p>An iframe is an inline frame, a frame that, while containing a completely separate page with its own URL, is nonetheless placed inside another HTML page. This gives very nice possibilities in web design. The problem is to access the iframe, for instance to load a new page into it. This page explains how to do it.</p> <p><strong>Frame or object?</strong></p> <p>The fundamental question is whether the iframe is seen as a frame or as an object.</p> <ul> <li>As explained on the <a href="http://web.archive.org/web/20080225210716/http://www.quirksmode.org/js/frameintro.html" rel="noreferrer">Introduction to frames</a> pages, if you use frames the browser creates a frame hierarchy for you (<code>top.frames[1].frames[2]</code> and such). Does the iframe fit into this frame hierarchy?</li> <li>Or does the browser see an iframe as just another object, an object that happens to have a src property? In that case we have to use a standard <a href="http://web.archive.org/web/20080225210716/http://www.quirksmode.org/js/dom.html" rel="noreferrer">DOM call</a> (like <code>document.getElementById('theiframe'))</code> to access it. In general browsers allow both views on 'real' (hard-coded) iframes, but generated iframes cannot be accessed as frames.</li> </ul> <p><strong>NAME attribute</strong></p> <p>The most important rule is to give any iframe you create a <code>name</code> attribute, even if you also use an <code>id</code>.</p> <pre><code>&lt;iframe src="iframe_page1.html" id="testiframe" name="testiframe"&gt;&lt;/iframe&gt; </code></pre> <p>Most browsers need the <code>name</code> attribute to make the iframe part of the frame hierarchy. Some browsers (notably Mozilla) need the <code>id</code> to make the iframe accessible as an object. By assigning both attributes to the iframe you keep your options open. But <code>name</code> is far more important than <code>id</code>.</p> <p><strong>Access</strong></p> <p>Either you access the iframe as an object and change its <code>src</code> or you access the iframe as a frame and change its <code>location.href</code>.</p> <p>document.getElementById('iframe_id').src = 'newpage.html'; frames['iframe_name'].location.href = 'newpage.html'; The frame syntax is slightly preferable because Opera 6 supports it but not the object syntax.</p> <p><strong>Accessing the iframe</strong></p> <p>So for a complete cross–browser experience you should give the iframe a name and use the</p> <pre><code>frames['testiframe'].location.href </code></pre> <p>syntax. As far as I know this always works.</p> <p><strong>Accessing the document</strong></p> <p>Accessing the document inside the iframe is quite simple, provided you use the <code>name</code> attribute. To count the number of links in the document in the iframe, do <a href="http://web.archive.org/web/20080225210716/http://www.quirksmode.org/js/iframe.html#" rel="noreferrer"><code>frames['testiframe'].document.links.length</code></a>.</p> <p><strong>Generated iframes</strong></p> <p>When you generate an iframe through the <a href="http://web.archive.org/web/20080225210716/http://www.quirksmode.org/dom/intro.html" rel="noreferrer">W3C DOM</a> the iframe is not immediately entered into the <code>frames</code> array, though, and the <code>frames['testiframe'].location.href</code> syntax will not work right away. The browser needs a little time before the iframe turns up in the array, time during which no script may run.</p> <p>The <code>document.getElementById('testiframe').src</code> syntax works fine in all circumstances.</p> <p>The <code>target</code> attribute of a link doesn't work either with generated iframes, except in Opera, even though I gave my generated iframe both a <code>name</code> and an <code>id</code>.</p> <p>The lack of <code>target</code> support means that you must use JavaScript to change the content of a generated iframe, but since you need JavaScript anyway to generate it in the first place, I don't see this as much of a problem.</p> <p><strong>Text size in iframes</strong></p> <p>A curious Explorer 6 only bug:</p> <p>When you change the text size through the View menu, text sizes in iframes are correctly changed. However, this browser does not change the line breaks in the original text, so that part of the text may become invisible, or line breaks may occur while the line could still hold another word.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload