Using Javascript to display a string variable in an HTML page is easy:
<html>
<body>
<script>
text = "hello"
document.getElementById("demo").innerHTML = text
</script>
<p id="demo"></p>
</body>
</html>
However I would like to replace "hello" with the contents of a local file, say file.txt
. I think this should be easily accomplished using Javascript's fetch()
API but I'm running into problems. This,
<html>
<body>
<p id="demo"></p>
<script>
text = fetch('file.txt').then(response => response.text())
document.getElementById("demo").innerHTML = text
</script>
</body>
</html>
will display [object Promise]
. I guess I somehow must access the response
object but I don't know how that is done.