Javascript iframe innerHTML为空
同样的代码昨天也在工作,iframe[0]。contentWindow.document.body.innerHTML的值为“success”。 但是,今天的iframe[0].contentWindow.document.body是“”。我找不到innerHTML。 iframe[0]的结果也是如此。contentDocument.body htmlJavascript iframe innerHTML为空,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,同样的代码昨天也在工作,iframe[0]。contentWindow.document.body.innerHTML的值为“success”。 但是,今天的iframe[0].contentWindow.document.body是“”。我找不到innerHTML。 iframe[0]的结果也是如此。contentDocument.body html <form id="formid" method="post" action="file/upload" enctype="multipa
<form id="formid" method="post" action="file/upload" enctype="multipart/form-data" target="frame">
<input id="" type="file" name="file" />
</form>
<iframe id="frame" name="frame" width="0px" height="0px" frameborder="0"></iframe>
使用iframe innerHtml的不同路径: JS: Html:
您是否试图在iframe中加载外部资源?在这种情况下,可能是由于安全原因,浏览器阻止访问。昨天我可以访问innerHtml,但您没有在
中加载任何文档。您需要在src
属性中指定URL。如果它将在同一个域中,那么您可以访问它的主体
和内部HTML
。我不会在iframe中加载任何内容。iframe正在将文件上载状态设置为成功或失败。我无法复制您试图执行的操作。表单提交和iframe之间有什么联系?如前所述,文件上载不会与iframe交互,iframe没有源,因此$(“#frame”)。永远不会调用load。我删去了以下要点中的表格:
var iframe = $('#frame');
document.getElementById("formid").submit();
$("#frame").ready(function(){
$("#frame").load(function () {
data = iframe[0].contentWindow.document.body.innerHTML;
if(data == "success"){
successFunction(); // calling function if success
}
});
});
function fileUpload(form, action_url) {
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "frame");
iframe.setAttribute("name", "frame");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("frameborder", "0");
form.parentNode.appendChild(iframe);
iframeId = document.getElementById("frame");
var eventHandler = function () {
if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
setTimeout('iframeId.parentNode.removeChild(iframeId)', 200);
}
if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);
form.setAttribute("target", "frame");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.submit();
}
<form>
<input type="file" name="file" /></br>
<input type="button" value="upload" onClick="fileUpload(this.form,'url'); return false;" >
</form>