「ウィジェット」と呼ぶものがあります...そしてiframe
、スクリプト タグの現在の場所にそのウィジェット (最終的には単なる ) を挿入したいと考えています。
だからあなたは持っているかもしれません:
<p>Some example text</p>
<script src="http://example.com/widget.js" class="widget" async></script>
<p>More text</p>
そのwidget.js
ファイルには、これがあります:
var createIframe = function() {
var parent = document.getElementsByClassName('widget');
var iframe = document.createElement('iframe');
iframe.src = '//example.com'
// Works, but just inserts it at the end
document.body.appendChild(iframe);
// Does not work...just doesn't seem to do anything
document.createElement("div").appendChild(iframe);
}
window.onload = function() {
createIframe();
}
コードのコメントに記載されているように、document.body.appendChild(iframe)
問題なく動作しますが、ドキュメントの最後に iframe を挿入するだけです。
しかし、私が本当にやりたいことは、script タグがある場所に iframe を挿入することです。それdocument.body.appendChild(iframe)
は私の試みですが、文字通り何もしていないようです(iframeもエラーもありません)。
では、スクリプト タグと同じ場所に iframe を挿入するにはどうすればよいでしょうか。