Printing .txt file into html

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Printing .txt file into html



I need to print a .txt file into a html document, I also need to be able to style this text output. I have seen many answers regarding this, but many have been poor, using things like PHP and "iframe". Would anyone know how to do this using JavaScript? (So that the data can be styled)



Many thanks





Can you edit the question to provide an example of the .txt file content you want, & also of how you want it styled please - as it stands the question is too broad for us to be able to help. Also it is not clear what you mean by 'PHP and .' - please clarify that as well.
– MandyShaw
Jul 29 at 19:19





The big problem here I think is to style the output, what do you mean with that? Is .txt raw text?
– Emeeus
Jul 29 at 19:24






@MandyShaw The styling is not the point of the question, sorry if that was unclear, I can do the styling myself. I just need a way of printing any .txt file, to html. PHP is a programming language, if you don't know what it is, no worries.
– Luke Robinson
2 days ago





You could use ajax, read this stackoverflow.com/questions/14446447/…
– Emeeus
2 days ago





It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask.
– Isma
2 days ago




1 Answer
1



Here you have a basic example:


<div id="myTXT">
<!-- your txt content goes here -->
</div>

<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)
document.getElementById("myTXT").innerHTML =
this.responseText;

;
xhttp.open("GET", "path/to/yourTxt.txt", true);
xhttp.send();
</script>



In this case, yourTxt.txt must be present in you server or in a server that allow cross origin, see this: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin





Sorry, what do you mean by in a server?
– Luke Robinson
2 days ago





Yes, here you have an exampe jsfiddle.net/ayt2c9od/4 I can't find a remote txt file but it's the same
– Emeeus
2 days ago






@LukeRobinson Let's suppose your javascript is in example.com/index.html, your .txt file must be in example.com/yourTxt.txt or in other domain but with allow origin headers
– Emeeus
2 days ago





I have read the whole Origin header page, but I'm not really too sure what it all means? I'm just trying to fetch a .txt file from a local file on my disk drive, is this possible? Or must it be off of a server?
– Luke Robinson
2 days ago





Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. So file:// is not supported. You can't read local files even if you share the origin (running javascript in localhost or somethig)
– Emeeus
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Mass disable jenkins jobs