iframe innerHTML为null [英] iframe innerHTML is null

查看:197
本文介绍了iframe innerHTML为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个代码昨天正常工作,iframe [0] .contentWindow.document.body.innerHTML的值为success。
但是,今天iframe [0] .contentWindow.document.body是。我找不到innerHTML。
同样是iframe [0] .contentDocument.body的结果。

this same code was working yesterday, iframe[0].contentWindow.document.body.innerHTML has value "success". But, today iframe[0].contentWindow.document.body is "". I cannot find innerHTML. Same is result of iframe[0].contentDocument.body.

html

<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>

js

 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
        }
        });
    });


推荐答案

使用iframe innerHtml的不同路径:

Using different path to iframe innerHtml worked:

JS:

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();
}

Html:

<form>
    <input type="file" name="file" /></br>
    <input type="button" value="upload" onClick="fileUpload(this.form,'url'); return false;" >
</form>

这篇关于iframe innerHTML为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆