IEで親ウィンドウに要素を追加する。
IEで親ウィンドウに要素を作成して追加する際に子ウィンドウのdocumentを使用するとうまくいかない。
親ウィンドウ
<table id="table"> <thead> <tr><th>label</th></tr> </thead> <tbody> </tbody> </table>
子ウィンドウJavaScript
function test() { var opener = window.opener; var tbody = opener.getElementById("table").getElementsByTagName("tbody"); var tr = document.createElement("tr"); var td = document.createElement("td"); td.innerText = "test" tr.appnedChild(td); tbody.appendChild(tr); }
この状態で実行してもtbody.appendChild(tr);
でエラーが発生します。
これを回避するには
function test() { var opener = window.opener; var doc = opener.document; var tbody = opener.getElementById("table").getElementsByTagName("tbody"); var tr = doc.createElement("tr"); var td = doc.createElement("td"); td.innerText = "test" tr.appnedChild(td); tbody.appendChild(tr); }
このように親ウィンドウのdocumentを使用してあげればうまくいきます。