javascript - Read text file and put result to innerHTML? - Stack Overflow
I want to use JavaScript to change the text of an element with Id 'foo'. The text it should change to should be taken from a file titled 'bar.txt' (can also be any other file format, as long as I get a string from the text inside.)
The text file and the HTML containing the script are in the same directory, and are visible to the client.
Searching for my problem on Google only led me to this link, which is for uploading a file to the website and isn't what I need.
I want to use JavaScript to change the text of an element with Id 'foo'. The text it should change to should be taken from a file titled 'bar.txt' (can also be any other file format, as long as I get a string from the text inside.)
The text file and the HTML containing the script are in the same directory, and are visible to the client.
Searching for my problem on Google only led me to this link, which is for uploading a file to the website and isn't what I need.
Share Improve this question asked 13 hours ago DevalartDevalart 632 silver badges10 bronze badges2 Answers
Reset to default 1To get the text from bar.txt
you need to make a GET
call using either XMLHttpRequest
or fetch
, whichever you prefer. Then you will need to set that text as the innerHTML
property of the element you're interested in.
Note you will not be able to perform a GET
call on your local machine without first spinning up a webserver.
Example:
foo.html
:
<!DOCTYPE html>
<html>
<head>
<title>Read from .txt</title>
</head>
<body>
<p>Below is the content of the text file:</p>
<p id="foo"></p>
<script>
fetch("bar.txt").then((response)=>{
return response.text();
}).then((text)=>{
document.getElementById('foo').innerHTML = text;
});
</script>
</body>
</html>
bar.txt
:
What I want for Christmas:
<ul>
<li>Candy</li>
<li>Toys</li>
<li>My two front teeth</li>
</ul>
What the browser renders:
There are really two questions here:
- How do I get the contents of a text file situated next to the HTML file.
- How to I update the text of an element with a given ID.
I'll assume you have a web server with these two files sitting next to one another:
my_page.html
→ the HTML file containing your page and scriptmy_text.txt
→ the text file
This script on your HTML page will load the contents of the file:
const response = await fetch("my_text.txt");
if (response.ok) {
// response.text() will return a promise with the file contents
} else {
// handle fetch failure
}
And this will update the element:
document.getElementById('foo').innerText = await response.text()
Here is a complete script with some rudimentary error reporting on the page:
const response = await fetch("my_text.txt");
if (response.ok) {
document.getElementById('foo').innerText = await response.text();
} else {
document.getElementById('foo').innerText = 'load failed!';
}
- 微软每年从安卓厂商获20亿美元专利费
- 分析称苹果本届WWDC将侧重软件 不会发布iPhone 5
- 仅售74美元的Android迷你电脑:你会买吗?
- 后PC时代是五足鼎立还是苹果独领风骚?
- Integration-testing a Spring Boot @Service without SpringBootApplication - Stack Overflow
- typescript - tag a href download filename nextjs - Stack Overflow
- Adding Fields to Order API Call on WooCommerce - Stack Overflow
- node.js - Cors errors S3 Upload node - Stack Overflow
- express - What is the point of http-only cookies and how to make your authentication secure? - Stack Overflow
- java - How to write custom Spring Security PreAuthorize annotation - Stack Overflow
- sql - Ranking query records in specific order - Stack Overflow
- python - Ipywidgets : How to get the list of comm opened in a jupyter notebook? - Stack Overflow
- html - Javascript cannot map object with repetative values - Stack Overflow
- c++ - How to create a class setup where two classes each holds an instance of the other? - Stack Overflow
- javascript - Why does this SVG filter not work in a canvas context using Chrome? - Stack Overflow
- azure - Unable to get a token to create B2C user using Graph API - Stack Overflow
- lean - Lean4 good way to solve `imports are out of date` - Stack Overflow