HTML5 with javascript to parse and display textfile - javascript

I have an HTML5 page (index.html), and I need to display the contents of a .txt file (p001wide.txt). Both are on the server in the same directory.
The text file is generated from a CMS, so I cannot change the format of that file.
In the .txt file is a variable named widetxt. I need to display the contents of the widetxt variable on the page.
What do I need to do to parse the the textfile and display it in the HTML page?
Do I need to use javascript, and if so, how?

Hm, I found this question which appears to be similar to yours as far as reading a file goes. As for parsing though why not use a database such as MySQL to store your data? This way you can quickly add and query through your data.

As both your index file and the text file you wish to read reside on the server you should be able to read the text file on the server and insert what you wish from it into the index file using PHP.
See http://www.w3schools.com/php/php_file.asp for a tutorial on how that can be done without resorting to client side script.
If you cannot alter your index file on the server and can put a PHP file on the server you can use AJAX to ask your PHP file to read the contents of the text file and return it to you. Then you would use javascript to insert it as you wish. I presume that you can alter the index file because otherwise you could not alter its javascript either.

You can do this with JavaScript, you basically need to learn how to read and write files (and you mention the variable inside, this would be parsing). Start reading here - http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm other information, just Google read write files javascript, or parsing text with javascript.
Best of luck!

Related

Is there no simple way to read a local JSON or a TEXT file using plain Javascript?

I have a local JSON file. I want to create an object of that file using JS.
I tried finding some solutions and they are simply too complicated
An answer suggests that "Being able to read a local file from your browser would be a major breach of security..." so does that mean that there is NO simple way to read a local file without using FileReader or XMLHttpRequest !?
I am very disappointed as I come from a Python background and there, you can simply use a one liner "open" function to read a file.
(PS: Please do not use any AJAX or jQuery in your answers/comments)
EDIT: Somewhat more detailed description.
I have 4 files in a folder. An HTML, a CSS, A JS and one JSON file. What I want is whenever, I try to load the HTML page in a browser, my JS creates a JSON object of my local JSON file which I can display in my HTML body.

Text file data into a webpage for graphing

I am new to web dev and I have a text file that I created using C# to collect some data from a website. Now I want to use that data to make graphs or some way to show the info on a website. Is it possible to use I/O in javascript or what is my best option here? Thanks in advance.
You have several options at your disposal:
Use a server-side technology (like ASP.Net, Node.js etc) to load, parse and display the file contents as HTML
Put the file on a web server and use AJAX to load and parse it. As #Quantastical suggested in his comment, convert the file to JSON forma for easir handling in Javascript.
Have the original program save the file in HTML format instead of text, and serve that page. You could just serve the txt file as is, but the user experience would be horrible.
Probably option 1 makes the most sense, with a combination of 1 + 2 to achieve some dynamic behavior the most recommended.
If you are working in C# and ASP then one option is to render the html from the server without need for javascript.
In C# the System.IO namespace gives access to the File object.
String thetext = File.ReadAllText(fileName);
or
String[] thetextLines = File.ReadAllLines(fileName);
or
If you have JSON or Xml in the file then you can also read and deserialize into an object for easier use.
When you have the text you can create the ASP/HTML elements with the data. A crude example would be:
HtmlGenericControl label = new HtmlGenericControl("div");
label.InnerHTML = theText;
Page.Controls.Add(label);
There are also HTMLEncode and HTMLDecode methods if you need them.
Of course that is a really crude example of loading the text at server and then adding Html to the Asp Page. Your question doesn't say where you want this processing to happen. Javascript might be better or a combination or C# and javascript.
Lastly to resolve a physical file path from a virtual path you can use HttpContext.Current.Server.MapPath(virtualPath). A physical path is required to use the File methods shown above.

Getting the file extension without knowing it's name

I need to know if there is a way to retrieve the real file extension without parse the filename.
In my code I split the file using the function split(filename, ".") then i get the last element of the array that the function returns.
Now, if I create a .pdf file called, for example test_file.pdf, the previous method works perfectly, but if i rename my file to test_file(without extension) I cannot retrieve the extension even if I know that the file is a PDF.
For example, if i rename test_file.pdf to text_file.jpg how can I recognize that the file is still a pdf and not an image file with .jpg extension?
I would like to know if there is a way to obtain this information, maybe using file metadata or other information related to the file.
I'm looking for a Javascript solution because I have to check the extension when I upload the file using a form (client side) but even a Java solution could be fine, can you help me?
Thank you in advance!
Look at this post and the marked answer: Get real file extension -Java code
I guess it's just what you need.

Retrieving Servlet info from non-form parts

I am trying to create an application that reads JSON strings.
Right now: I can input a JSON string through Java, write it to an HTML document and have a JavaScript application read it; which then parses it and writes it to the same HTML application. I need to know how, using Java, to read the HTML that it gets written to so I can use that data. It is important to note this HTML file is all generated by code so there is no actual text file to read.
I realize this is a roundabout way of doing it, but up until this point it has worked. My question is simple: How can I read an HTML page in a part that is not in a <form> through either regular Java or Servlet.
You can do that only by parsing the HTML in Java. And, there are some open source libraries that does this job for you.
Here is one that you can use.
http://jsoup.org/

How to load file contents from another domain using just JS?

I'm thinking of doing some online file manipulation for mobile users, the idea being that the user provides a URL to the file, then the file contents are modified by the JS, and can then be downloaded. But I haven't been able to figure out how to get the file when it's on a separate domain using just JS.
Is this possible? If so any hints or examples would be appreciated.
Just wanted to add that part of what I wanted to do was make it available without my hosting it. I'm thinking of something like a a file they can host somewhere,and then all of the bandwidth is their own...and that of wherever they are getting the file from of course.
The only way to load contents of a file on another domain is from within a <script> tag. This is how JSONP works. Look into getting your target file into this format.
The other way would be to use a local proxy. Create a web service method that loads and returns the contents of the file, then call that locally using your favorite JavaScript framework.
Depending on how you think of public webservices, and within some limitations I'm still mapping, you can do this using an ajax call to YQL, like so.
(will expand the answer later).
http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20data.uri%20where%20url=%22http://t3.gstatic.com/images?q=tbn:ANd9GcSyART8OudfFJQ5oBplmhZ6HIIlougzPgwQ9qcgknK8_tivdW0EOg%22
One of the limitations of this method is file size, it currently tops out at 25k.

Categories