我正在尝试读取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;
}
的test.txt
Iron
Aluminum
Steel
//etc. for x number of times (depends on data I have parsed previously)
我希望JavaScript能够读取test.txt并将其解析为数组(text
)。问题是,当我点击“试一试”按钮时,弹出警报(告诉我正在调用该函数),text
包含null
。我正在运行计算机上的所有文件,并且所有文件都在完全相同的目录中。