APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

Can EjsS (javascript) read or write a file? post and replies

Return to the Can EjsS (javascript) read or write a file? thread
Login to post to this thread

Can EjsS (javascript) read or write a file?
Chi-Feng Lin
28 Posts

Dear Paco

Can EjsS (javascript) read or write a file from disk or internet? I am trying to import a data file to EjsS and to display picture using Mesh2D function.


Could I have your suggestion on this? I'd be grateful for your help.

Thank you.


Chi-Feng


Replies to Can EjsS (javascript) read or write a file?

Re: Can EjsS (javascript) read or write a file? -
Francisco Esquembre
228 Posts

His,

The following code will let you read a file from a URL.
We just tested it an it works.

Paco


var xmlhttp = new XMLHttpRequest();
var url = "http://fem.um.es/tmp/state.txt";

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      console.log (xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();



Re: Can EjsS (javascript) read or write a file? -
Francisco Esquembre
228 Posts

Hi again,

This, more sophisticated, code guesses the path to the file, if located next to the HTML simulation file.

Paco

var xmlhttp = new XMLHttpRequest();
//var url = "http://fem.um.es/tmp/state.txt";

var url = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
var pathArray = url.split( '/' );
var secondLevelLocation = pathArray[0];
var newPathname = "";
for (i = 1; i < pathArray.length; i++) {
  newPathname += pathArray[i-1];
  newPathname += "/";
}
  newPathname += "state.txt";

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      console.log (xmlhttp.responseText);
        _unserialize(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", newPathname, true);
xmlhttp.send();



Re: Re: Can EjsS (javascript) read or write a file? -
Chi-Feng Lin
28 Posts

Dear Paco

Thank you for your help. I will try it.

Chi-Feng



Re: Re: Can EjsS (javascript) read or write a file? -
Chi-Feng Lin
28 Posts

Dear Paco

Thank you for your codes. But I still have no idea to write those codes in EjsS.

Can you provide an EjsS example about reading and writing data especial for Mesh2D?

I appreciate your help deeply.

Chi-Feng



OSP Projects:
Open Source Physics - EJS Modeling
Tracker
Physlet Physics
Physlet Quantum Physics
STP Book