通过javascript(书签):我需要创建一个iframe然后添加一个表单,然后添加一个提交表单的脚本。
以下适用于Chrome,但不适用于IE:
var i = document.createElement('iframe'); // also give it id = iframe_id
var frm = document.createElement('form'); // also add inputs and values
var s = document.createElement('script'); // also add innerHTML that submits form
document.body.appendChild(i);
window.frames['iframe_id'].document.getElementsByTagName('body')[0].appendChild(frm);
window.frames['iframe_id'].document.getElementsByTagName('body')[0].appendChild(s);
在IE中我收到一个错误:无法获取未定义或空引用的属性'appendChild'
在将iframe添加到文档之前,我尝试将表单和脚本附加到iframe:
i.appendChild(frm);
i.appendChild(s);
document.body.appendChild(i);
我在Chrome中得到了一个奇怪的回应。表单提交的响应显示在阻止的弹出窗口中(而不是完全没有,我期望)。在IE中似乎没有什么事情发生。
答案 0 :(得分:5)
我建议最初使用write()
直接写入文档。
演示:http://jsfiddle.net/QjPuZ/1/
代码:
var iframeDoc = i.contentDocument || i.contentWindow.document;
iframeDoc.open();
iframeDoc.write("<html><body></body></html>");
iframeDoc.close();
iframeBody = iframeDoc.body;
var frm = iframeDoc.createElement('form');
var s = iframeDoc.createElement('script');
iframeBody.appendChild(frm);
iframeBody.appendChild(s);
答案 1 :(得分:0)
你的例子不起作用只是因为需要在框架内创建元素。
var o = iframeDoc.createElement('form');
iframeDoc.appendChild(o); // "body" is not present in IE, but it works