2011-08-15 4 views
0

私はいくつかの非常に奇妙な行動を取得しています:(私は、誰かが文書でのJavaScriptファイルを取得してXMLHTTPリクエストイムを使用してJavaScriptの..のdocument.write ..奇妙な文書の振る舞い: '(

を助けることを願って書いて、それ。このようなものになります。

document.write("<input type='hidden' name='orange' value='17' />"); 
document.write("<input type='hidden' name='apple' value='29' />"); 

を私は基本的にはiframe内にある形でこれらの入力要素を追加したい

// i get the document of the iframe 
var docc = doc.getElementById("rs-iframe").contentDocument; 
var body = docc.getElementsByTagName('body')[0]; 

// i put the response from xmlhttprequest in a script tag 
var script = docc.createElement('script'); 
script.type = "text/javascript"; 
script.text = "//<![CDATA["+xmlhttp.responseText+"//]]>"; 

// i get the position where i want to put my script tag 
var elem = form.getElementsByTagName('script')[6]; 

// i try to insert my script tag from xmlhttprequest before the script i retrieve from the form 
elem.parentNode.insertBefore(script, elem); 

// the i append the form to the body of the iframe document      
body.appendChild(form); 

今私はdoc.getElementsByTagName('input');私を取得しようとします。 document.writeから追加された要素だけを取得し、他のフォーム要素は消えてしまった:(

私はすべての助けを感謝します。

答えて

4

これはwrite()が行っていることですが、文書の中に書きます。ドキュメントが既に閉じられている(閉じられているとは完全に読み込まれている)場合、write()はがドキュメントを上書きします。

解決策は簡単です。ドキュメントが既に読み込まれているときにwrite()を使用しないでください。新しいノードを挿入するには、appendChild()やinsertBefore()などのDOMメソッドを使用します。