Javascript reading files: how should I have asked?

Click For Summary

Discussion Overview

The discussion revolves around the challenges of using JavaScript to read a data file of numbers into an array, particularly in a local web environment. Participants explore the context of the original question, potential improvements in phrasing, and alternative approaches to achieve the desired outcome without relying on server-side solutions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes a workaround using MATLAB to generate a JavaScript function that contains the data array, which is then called from an HTML file.
  • Another participant notes that the original question lacked clarity and suggests that asking about processing data in MATLAB and displaying it on a web page might have yielded better responses.
  • It is mentioned that JavaScript traditionally operates in a sandbox environment in browsers, which complicates local file access.
  • NodeJS is proposed as a solution for server-side JavaScript that can read and write files, allowing for more flexibility in data handling.
  • A suggestion is made to rephrase the original question to focus on loading a data file into an HTML page for use with a 3D renderer, emphasizing the need for a local solution.
  • Concerns are raised about the limitations of XMLHttpRequest when dealing with local files, as it cannot access data in the user sandbox.
  • Clarification is provided regarding the flow of data from a server to the browser, specifically about how JSON data is sent back in response to requests.

Areas of Agreement / Disagreement

Participants express differing views on how to effectively phrase the original question and the feasibility of accessing local files with JavaScript. There is no consensus on a single approach or solution, as multiple perspectives on the issue are presented.

Contextual Notes

Limitations include the constraints of JavaScript in a browser environment regarding local file access, the need for server-side processing in certain scenarios, and the ambiguity in the original question that may have led to less helpful responses.

Trying2Learn
Messages
375
Reaction score
57
TL;DR
IO in javascript: what is the question
Hello

A few weeks ago (March 7) I asked how to have Javascript read a data file of numbers into an array.

I now understand why this is such a challenge when running a code on a web page, locally.

I have since found a sufficient workaround. I hope I can share it in the hope that someone can tell me how I SHOULD have asked the question. (Yes, this is an odd question: I am actually asking FOR a question.)

Here is what I did.

After Matlab processed all the numbers, I had Matlab open a file named "Rotation.js"
Matlab wrote the lines into Rotation.js to create an open of a function... then it deposited the data array.
Then it wrote the lines to "close the function."
Then it closed the file.

Then I ran the *.html file that ran the thereJS code. The main.js called the function that Matlab created, and displayed the
results in 3D.

So... I know that is a silly thing to have done (I KNOW I could have coded in OpenGL, or written the javascript code to process
the data. But considering the complexity of the Runge-Kutta method on six differential equations, this was a good workaround.

What SHOULD I have asked to have gotten this result? Again, i am not asking for an answer or even a better way. I just want to know how I could have asked the question.

Here is the section from Matlab to show you what I did.fileID = fopen('get_RotationMatrix.js','w');
fprintf(fileID,'function get_RotationMatrix(){\n');
fprintf(fileID,'var rotation = [\n');
for i = 1:timeIntegrationSteps
fprintf(fileID,'\"%12.8f\",\n',omega1(i));
end
fprintf(fileID,'];\n');
fprintf(fileID,'return rotation;\n}\n');
fclose(fileID);
 
Technology news on Phys.org
Nice workaround. In essence, you used a server-side program to create the necessary javascript containing your data that could then be retrieved by the browser and then processed on your web page.

In the context of javascript, your question is without meaning. I suppose if you had asked about processing data in Matlab and then how to display it on a web page you might gotten better responses but we'll never know as programming is such a diverse platform of recipes to do the same thing in different ways.

The problem with your question is that:
1) you are using javascript that traditionally works in a sandbox on a browser where local files simply can't be read or written.
2) you need to do things outside the browser to get it to work

NodeJS is a framework/server where you can use javascript on both the server-side and browser-side. In this case, javascript running on NodeJS can access files to read and to write.

You might read your binary data, compute stuff and then write out a JSON file with the data. There are methods that write data in JSON format for you.

The browser side of your application could request a computation and then retrieve the JSON file to process and display.
 
  • Like
Likes   Reactions: Trying2Learn and FactChecker
Trying2Learn said:
What SHOULD I have asked to have gotten this result?
How about "I want to load a data file into an HTML page (so I can use it as input to the 3D renderer Three.js), how can I do this? I want all this to be local, not through a web server or anything."
 
  • Like
Likes   Reactions: Trying2Learn
jedishrfu said:
The browser side of your application could request a computation and then retrieve the JSON file to process and display.
That won't work with a local file, the XMLHttpRequest api won't work with file:// and although a script tag will happily parse a JSON file there is no way to access the data in the user sandbox.
 
Last edited:
My apologies, that’s not what I meant the json data is sent back to the browser via the response to the request.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
18K