-1

Apologies, I'm sure there is a simple solution for this. But after 2 days searching, I can't find the right answer.

The basics: I want to pull a string from a plain text file (.txt) that is live and online, uploaded to my server (src="example.txt") and insert that string into a webpage on that same site ("page.html").

More details: My site is very basic. It allows readers (language learners) to listen to audio of a reader, while the site highlights the passages being read. So the plain text files in question are text/audio timing codes for dialog subtitles, and I'm trying to automatically generate the HTML from them so I don't have to insert the text + span codes into each HTML page individually (there are a lot of them). I have a very simple script that turns the subtitle timing codes into simple tags for HTML, and those text strings are what need to be read from the txt file, and inserted into a p tag in the HTML page. There is nothing else in the txt file.

The closest result I have found to my query is here. The and solutions are no good, as I don't want the content embedded — I need the unicode text string inserted in my HTML so it can be read by the script for the read-along highlighting. The Javascript suggested here, upon testing, doesn't work.

dirk
  • 1

1 Answers1

0

Your linked question makes use of innerHTML, which can have issues when using XML escaping or XML tags, e.g. <test>, since it will get seen as HTML. (valid XML tags are also valid HTML tags)

Assuming that is the issue you're stumbling on (there isn't much else to go off on), you can use innerText instead, which prevents the text to be interpreted as HTML.

If that isn't your problem, you'll have to be more specific about the exact issue you encountered in your code. It also isn't fully clear whether your .txt files contain HTML tags, e.g. <p>, or purely text.

Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
  • Here is a test JSFiddle where I have been playing with the codes. I haven't been able to get them to work: https://jsfiddle.net/L8g3qhtj/ The test txt I'm using there doesn't have any HTML tags. You can see I loaded it there as an to check it. The JS codes don't insert anything into the
    s I've set up w/ the id...
    – dirk Jul 18 '21 at 17:38
  • It looks like me that you're not actually performing the request. Mismatched brackets or something? Your `open` and `send` calls happen inside the `onreadystatechange` handler. – Kelvin Schoofs Jul 18 '21 at 17:49
  • I was looking at the code at the bottom, but looks like it's running the code embedded in the HTML, which is similarly broken. Also once I did fix it, it still actually failed because of CORS issues, making the GET request "fail" anyway. – Kelvin Schoofs Jul 18 '21 at 17:55