APS Excellence in Physics Education Award
November 2019
Science SPORE Prize
November 2011
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?
March 3, 2015 3:12AM UTC
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
Re: Can EjsS (javascript) read or write a file? -
Mar 5 5:51AM UTC
Francisco Esquembre
237 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? -
Mar 5 5:52AM UTC
Francisco Esquembre
237 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? -
Mar 8 8:12PM UTC
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? -
May 11 9:39PM UTC
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