2

我正在尝试读取 HTML 和 JavaScript 文件的同一目录中的文件,但它似乎返回 null。下面我添加了每个文件中的代码。

HTML 文件:

<html>
    <!-- Code to call the Google Maps API and link style.css sheet -->
<body>
    <div class="content">
        <div id="googleMap"></div>
        <div id="right_pane_results">hi</div>
        <div id="bottom_pane_options">
            <button onclick="get_parameters()">Try It</button>
        </div>
    </div>
</body>
<script type="text/javascript" src="./javascript.js">
    </script>
</html>

JavaScript 文件:

function get_parameters() {
    alert("hi"); // Just to let me know the function is getting called
    var freader = new FileReader();

    var text;
    freader.onload = function(e) {
        text = freader.result; 
    }
    freader.readAsText('./test.txt', "ISO-8859-1");
    text = freader.result; // To my knowledge, this should be taking the current line freader is on and storing it into text

    var div = document.getElementById('bottom_pane_options');
    div.innerHTML = div.innerHTML + text;
}

测试.txt

Iron
Aluminum
Steel
//etc. for x number of times (depends on data I have parsed previously)

我想要的只是让 JavaScript 读取 test.txt 并将其解析为一个数组(text)。问题是,当我单击“尝试”按钮时,会弹出警报(告诉我正在调用该函数)并text包含null. 我正在从我的计算机上运行所有文件,并且所有文件都在完全相同的目录中。

4

0 回答 0