读取本地文件时,文件未在Javascript中定义

时间:2015-09-12 21:01:55

标签: javascript html5

<html>
<head>
<title>My first styled page</title>
</head> 
<body>

<script> 
var reader = new FileReader();
reader.onload = function(){
var text = reader.result;
document.querySelector('body').append(text);
};
reader.readAsText(README, "us-ascii");
</script>

<!-- Site navigation menu -->
<ul class="navbar">
<li><a href="alamode-fetch">Alamode</a>
<li><a href="README">Readme</a>
<li><a href="town.html">My town</a>
<li><a href="links.html">Links</a>
</ul>

<!-- Main content -->
<h1>My first styled page</h1>
<p>Welcome to my styled page!
<p>It lacks images, but at least it has style.And it has links, even if they       don't go anywhere&hellip;
<p>There should be more here, but I don't know what yet.
<!-- Sign and date the page, it's only polite! -->
<address>Made 5 April 2004<br> 
by myself.
</address>
</body>
</html>

以上是我的html文件。我试图读取名为README的本地文件。当我执行我的html文件时,看起来我的javascript没有任何反应。我发现在firefox中,它提醒README没有定义。我已经坚持了一整天......

1 个答案:

答案 0 :(得分:0)

问题是README。 Javascript期望README将是一个变量,但您没有定义它。

查看https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText