getElementById有时为null.已经试过了.加载完毕.准备好了


getElementById is sometimes null. Already tried .onload and .ready

我有一些代码,可以在新窗口中显示html、xml或txt文件中的内容。我遇到了一个问题,getElementById有时会返回null。它似乎通常在Chrome中工作,但IE8,尤其是Firefox非常不稳定。

更具体地说,一旦我试图显示txt文件,它就不起作用了。

我已经尝试添加(在我的情况下)newWindow.onload=function(){。。。和$(newWindow.deocument).ready(function(){。。。尽管如此,由于我是javascript和php的新手,我可能做错了。

以下是相关代码:

Javascript:

function newWindow(pathname, type){
    var newWindow = window.open("newWindow.php");
    $.ajax({
            type: "post",
            url: "windowHelper.php",
            data: {pathname: pathname, type: type},
            dataType: "json"
    })
    .done(function(data, textStatus, jqXHR){  //tried to add the .onload and .ready here
            newWindow.document.getElementById("newWindow").innerHTML = data.data1;
     })
    .fail(function(jqXHR, textStatus, errorThrown){
            newWindow.document.getElementById("newWindow").innerHTML = textStatus + errorThrown;
    });
}

php:

        if(isset($_POST["pathname"]) && !empty($_POST["pathname"]) && isset($_POST["type"]) && !empty($_POST["type"])){
            $pathname = $_POST["pathname"];
            $type = $_POST["type"];
            $filename = str_replace("/var/tmp/ciscat/", "", $pathname);
                    if($type == 'html'){
                            $contentString = str_replace("<html>", "", file_get_contents($pathname));
                            $contentString = str_replace("</html>", "", file_get_contents($pathname));
                            $contentString = str_replace("<body>", "", file_get_contents($pathname));
                            $contentString = str_replace("</body>", "", file_get_contents($pathname));
                            }
                    else if($type == 'xml'){
                            $contentString = htmlspecialchars(file_get_contents($pathname), ENT_QUOTES);
                    }
                    else if($type == 'txt'){
                            $contentString = str_replace("'n", "<br/ >", file_get_contents($pathname));
                    }
                    else{
                            $contentString = "Blir feeeeel!!";
                    }
            $reportContent = "<p class = 'normalText'>Content of the file: $filename. Download the file in order to utilise the full content. </p> $contentString";
            print json_encode(array( "data1" => $reportContent));
    }

HTML:

<!DOCTYPE html>
<html>
<head>
<SCRIPT type="javascript/text" src="functions.js"></SCRIPT>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id = "newWindow">
</div>
</body>
</html>

有人有什么其他想法我可以试试吗?如有任何帮助,我们将不胜感激!:)

我怀疑这里可能有一些"糟糕"的代码,对ofc的一些评论也很好,但我的主要问题是evul getElementById。

提前感谢

[编辑]

我想你可以试着这样包装$.ajax()(jQuery仍然应该首先添加^_^):

$.ajax({
    type: "post",
    url: "windowHelper.php",
    data: {pathname: pathname, type: type},
    dataType: "json",
    success: function(data, textStatus, jqXHR) {
        newWindow.document.getElementById("newWindow").innerHTML = data.data1;
    },
    error: function(data, textStatus, errorThrown) {
        newWindow.document.getElementById("newWindow").innerHTML = textStatus + errorThrown;
    }
})

首先添加jQuery,

而不是这个

 newWindow.document.getElementById("newWindow").innerHTML = data.data1;

你能用这个吗

 $("#newWindow").html(data.data1);

希望这能有所帮助。

好吧,由于我无法获得其他任何东西,包括.onload和.ready(这两个反而破坏了其他东西),我决定对我的问题进行一点"丑陋"的修复。

标签,例如newWindow,在我的Javascript之前没有时间加载,因此getElementById("newWindow)返回null,因为ID还不存在。

我所做的是添加一个计时器,在发布到新窗口之前等待0.5秒,确保它有时间加载。这是有效的,但这不是一个好的答案,因为当新窗口加载非常缓慢时(我怀疑),它可能会失败。然而,它确实有效,在缺乏更好的选择的情况下,这就是我将要做的

除了计时器之外,我还更改了HTML文档中标记的顺序,并更改了变量名,以确保没有两个东西的名称相同。这些并没有解决我的问题,但它是很好的实践。

新JS代码:

function openNewWindow(pathname, type){
    /*newWindow has to be defined here*/
    var popWindow = window.open("newWindow.php");

    $.ajax({
            type: "post",
            url: "windowHelper.php",
            data: {pathname: pathname, type: type},
            dataType: "json"
    })
    .done(function(data, textStatus, jqXHR){
            setTimeout(function() {
            popWindow.document.getElementById("newWindowDiv").innerHTML = data.data1;
            }, 500);
     })
    .fail(function(jqXHR, textStatus, errorThrown){
            setTimeout(function() {
            popWindow.document.getElementById("newWindowDiv").innerHTML = textStatus + errorThrown;
            }, 500);
});
}