Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #32303
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Read in file to array. |
| Date | 2017-01-24 17:21 +0000 |
| Message-ID | <240120171721187975%timstreater@greenbee.net> (permalink) |
| References | <2b6a77ee-b20d-4f11-bd56-c73f66523ec7@googlegroups.com> <015aa987-b329-446c-9fb9-b7c1317a7a39@googlegroups.com> <30277451-ca92-4bc3-b070-3ed4fd8d2dfc@googlegroups.com> |
In article <30277451-ca92-4bc3-b070-3ed4fd8d2dfc@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:
[snip]
>Well now the gist seem to work but there must be some problem with javascript
>I manage to get an alert on the data if i "load twice", isn't that strange.
>
>alert(textFromFileLoaded);
>sub=textFromFileLoaded.split("!");
>
>"X3LOADSAVE.js:46 Uncaught ReferenceError: textFromFileLoaded is not defined
> at loadGraph (X3LOADSAVE.js:46)
> at HTMLButtonElement.onclick (0GRAPH.HTML?filename=N8L3circle.txt:31)"
>
>But second time file loaded it is read into array without errors reported, it
>seem like a timing issue?
>
>There is no problem to just write to the textarea first time but for some
>reason i can not use textFromFileload at all i must copy the value to my own
>variable and even then it will not be read in the first time i load, but the
>second time i press button to load it work.
>
>function loadGraph()
>{
> textFromFileLoaded="";
> var fileToLoad = document.getElementById("fileToLoad").files[0];
> var fileReader = new FileReader();
> fileReader.onload = function(fileLoadedEvent)
> {
> textFromFileLoaded = fileLoadedEvent.target.result;
> document.debug.data.value = textFromFileLoaded;
> mytext=textFromFileLoaded;
> };
> fileReader.readAsText(fileToLoad, "UTF-8");
> //arr=[];
>
> alert(mytext);
> sub=mytext.split("!");
> for (var x=0;x<sub.length;sub++){
> dataitem=sub[x].split("*");
> alert(dataitem[1]);
> for(var a=0;a<dataitem.length;a++){
> arr[a].hold=false;
> arr[a].rposX=parseInt(dataitem[4]);
> arr[a].rposY=parseInt(dataitem[5]);
> arr[a].rwidth=parseInt(dataitem[6]);
> arr[a].rheigth=parseInt(dataitem[7]);
> arr[a].rcolor=dataitem[8];
> arr[a].bcolor=dataitem[9];
> links=dataitem[1].split(",");
> for (var b=0;b<dataitem;b++) {
> arr[a].nodelinks[b]=parseInt(links[b]);
> }
> }
> }
>}
I'm not at all familiar with the FileReader functions, but if you have
to create a function to handle an onload event, then I'm guessing that
readAsText must be asynchronous. So, what it does is *start* the read,
and then returns immediately. You then try and do something with mytext
but nothing has yet been put in it. So you get an error. Later, when
the read is complete, the onload fires and puts data into mytext.
When you run the second time, mytext contains the result of the first
load so things seem to work.
You need to put all the code to do something with mytext into the
onload function.
--
"Once you adopt the unix paradigm, the variants cease to be a problem - you
bitch, of course, but that's because bitching is fun, unlike M$ OS's, where
bitching is required to keep your head from exploding." - S Stremler in afc
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread
Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 03:29 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 03:59 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 06:26 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 07:41 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 08:08 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 08:43 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 09:41 -0800
Re: Read in file to array. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-23 10:29 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:24 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:38 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:56 -0800
Re: Read in file to array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-23 20:36 +0000
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 19:54 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 20:10 -0800
Re: Read in file to array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-24 14:27 +0000
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 08:24 -0800
Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-24 17:42 +0100
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 08:54 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 09:04 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 11:07 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 13:17 -0800
Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-24 21:35 +0000
Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-24 22:42 +0100
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 15:32 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 15:55 -0800
Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-25 08:59 +0000
Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-25 01:58 +0100
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 18:51 -0800
Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-25 10:29 +0100
Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-25 08:58 +0000
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 12:28 -0800
Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-24 17:21 +0000
csiph-web