 <p>You need to <a href="http://stackoverflow.com/questions/403967/how-may-i-reference-the-script-tag-that-loaded-the-currently-executing-script">get the current <code>&lt;script&gt;</code> tag</a>, which in this case is the last one:</p> <pre><code>var thisScript = document.scripts[document.scripts.length - 1]; </code></pre> <p>and <a href="https://developer.mozilla.org/en-US/docs/DOM/Node.insertBefore">insert</a> the <code>&lt;iframe&gt;</code> after it:</p> <pre><code>var parent = thisScript.parentElement; parent.insertBefore(iframe, thisScript.nextSibling); </code></pre> <p><a href="http://jsfiddle.net/mattball/Tz75x/">http://jsfiddle.net/mattball/Tz75x/</a></p> <p>Or simply <a href="https://developer.mozilla.org/en-US/docs/DOM/Node.replaceChild"><em>replace</em></a> the current script with the <code>&lt;iframe&gt;</code>:</p> <pre><code>var parent = thisScript.parentElement; parent.replaceChild(iframe, thisScript); </code></pre> <p><a href="http://jsfiddle.net/mattball/a23XE/">http://jsfiddle.net/mattball/a23XE/</a></p> <hr> <p>Alternately – if you can believe it – <strong><a href="https://developer.mozilla.org/en-US/docs/DOM/document.write"><code>document.write()</code></a> is actually a suitable solution here</strong>, so long as you do it <em>while the document is loading</em> and not in a <code>window.onload</code> callback:</p> <pre><code>function createIframe() { document.write('&lt;iframe src="//example.com"&gt;&lt;/iframe&gt;'); } createIframe(); </code></pre> <p><a href="http://jsfiddle.net/mattball/2YCcP">http://jsfiddle.net/mattball/2YCcP</a></p>