未捕获的类型错误:无法读取 Firefox 和 Chrome 中的属性“innerHTML”

Uncaught TypeError: Cannot read property 'innerHTML' in Firefox and Chrome

本文关键字:Chrome 属性 innerHTML Firefox 类型 错误 读取      更新时间:2023-09-26

我在JS的站点中有文件上传代码:

function detectDone(){
done++;
var _ifrWallp = document.getElementById("ifrWallp");
var html = _ifrWallp.contentWindow.document.body.innerHTML;
if (html.indexOf('File name:') != -1) {
    // Done
    uploadedWall();
} else {
    setTimeout('detectDone()',10);
}}

和 HTML:

<div id="addWallpp" class="addMWallp" style="float: left;" onmouseover="document.getElementById('uploadDesc').style.display = 'block'; document.getElementById('ifrWallp').contentWindow.document.getElementById('wpmain').style.backgroundColor = '#FFF7C0';" onmouseout="document.getElementById('uploadDesc').style.display = 'none'; document.getElementById('ifrWallp').contentWindow.document.getElementById('wpmain').style.backgroundColor = '#F9F9F9';">
    <iframe id="ifrWallp" onload="Upload('../')" scrolling="no" frameborder="0" style="text-align:center;vertical-align:middle;border-style:none;margin:0px;width:100%;height:45px" src="sources/private/upload.php"></iframe>

和 PHP 文件中的一些代码。在IE中我没有错误,但是chrome和Firefox在行中给出了错误:

var html = _ifrWallp.contentWindow.document.body.innerHTML;

未捕获的类型错误:无法读取 null 的属性"innerHTML"

在FF中:

类型错误: _ifrWall.contentWindow.document.body 为空

我知道我必须在页面加载后加载 iframe 代码,但不幸的是我不知道如何让它与我的文件上传一起使用。请帮忙:(

请参阅此处的说明和代码示例:http://www.sitepoint.com/forums/showthread.php?405244-InnerHTML-and-Firefox

基本上contentWindow是一个IE的东西。对于 FF,您需要使用 contentDocument。

//ff
if (_ifrWallp.contentDocument){
var html=_ifrWallp.contentDocument.getElementsByTagName("BODY")[0].innerHTML;
}
//ie
else if (_ifrWallp.contentWindow){
var html=_ifrWallp.contentWindow.document.body.innerHTML;
 }
// if (html.indexOf('File name:') ... etc