用意されているDivタグ内に、javascriptでscriptタグ(外部js)を挿入しようとしたところ、
下記のエラーとなりました。
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened
そのため、以下のコードでiframeを作成し、その中で外部jsを実行させようとしたのですが
上手く実行されない状況です。
どのように修正したらよろしいでしょうか。
function createIframe(id, script_url) { //iframeをつくる var iframe = document.createElement('iframe'); document.getElementById(id).appendChild(iframe); var html = ''; html += '<body>'; html += '<script src="' + script_url + '"></script>'; html += '</body>'; var doc = iframe.contentWindow.document; doc.open(); doc.write(html); doc.close(); }
※
id→Divタグのid
script_url→外部jsのurl
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。