新しいウィンドウ内に html、xml、または txt ファイルのコンテンツを表示するコードがあります。getElementById が null を返すことがあるという問題があります。通常は Chrome で動作するようですが、IE8、特に Firefox は非常に不安定です。
具体的には、txt ファイルを表示しようとすると機能しません。
私はすでに (私の場合) newWindow.onload = function(){ ... と $(newWindow.document).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>
私が試すことができる追加のアイデアはありますか? どんな助けでも大歓迎です!:)
ここには少し「悪い」コードがあるのではないかと思います。それについてのコメントもいいでしょうが、私の主な問題は evul getElementById です。
前もって感謝します