Experts Round Table Network

Clientside Technology => Javascript => Topic started by: rdivilbiss on June 20, 2007, 06:25:06 PM



Title: Reading Files With JavaScript
Post by: rdivilbiss on June 20, 2007, 06:25:06 PM
Regarding my article: http://www.expertsrt.net/main/, the same (give or take) code in the page at http://www.rodsdot.com/ee/clientSideInclude2.asp (http://www.rodsdot.com/ee/clientSideInclude2.asp) was throwing errors in older AppleWebKit engine browsers.

I recently treaked the coded at: http://www.rodsdot.com/ee/clientSideInclude2.asp (http://www.rodsdot.com/ee/clientSideInclude2.asp).

Is there any one with access to Safari or another WebKit engine browser who can advise if the page is giving errors?

(I can't get Safari for Windows to work, not that would have been an exhaustive test.)

Thanks,
Rod


Title: Re: Reading Files With JavaScript
Post by: GrandSchtroumpf on June 21, 2007, 06:45:45 AM
http://www.rodsdot.com/ee/clientSideInclude2.asp.
The page hangs at 79% load in my Koquerror 3.5.2.
The status bar says "8 images of 11 loaded".


Title: Re: Reading Files With JavaScript
Post by: rdivilbiss on June 21, 2007, 10:27:54 AM
There are problems with it for sure.


Title: Re: Reading Files With JavaScript
Post by: rdivilbiss on June 22, 2007, 07:11:44 PM
http://www.rodsdot.com/ee/clientSideInclude2.asp.
The page hangs at 79% load in my Koquerror 3.5.2.
The status bar says "8 images of 11 loaded".

I believe your experience was based on the fact that my server was under a DDOS attack.


Title: Re: Reading Files With JavaScript
Post by: morchuboo on August 22, 2007, 10:20:04 AM
Great article.
I have zero knowledge of javascript so forgive me if my question is silly:

Is it possible to load the text into javascript variables instead of a page element?
I have a page that is used to do a map mashup and want to load postcodes from a text file (csv) into an array of variables so I can pass them to another function that already successfully does postcode lookups and plots them on the map.

Thanks
M


Title: Re: Reading Files With JavaScript
Post by: GrandSchtroumpf on August 22, 2007, 03:27:44 PM
Your question is only sightly silly.
The text is read into a javascript variable.  Then javascript is used to add the content of the variable to the document.
This means you don't need to do anything additional to have what you requested.

Cheers


Title: Re: Reading Files With JavaScript
Post by: rdivilbiss on August 22, 2007, 08:04:25 PM
GS is correct.

To be more specific:

Code:
function handleHttpResponse(pObjRequest, pElementId) {
    if (pObjRequest.readyState==4) {
        if (pObjRequest.status==200) {
            document.getElementById(pElementId).innerHTML=pObjRequest.responseText;
            pObjRequest=null;
        }
    }
}

pObjRequest.responseText  contains your zip codes from your CSV file.

You will have one large string with the file contents so you'll need to parse it, preferably into an array.

Without the file structure I can't give you a concrete example, but assuming this:

"99925","55.552611","-133.0555"CRLF
"99925","55.552611","-133.0555"CRLF
"99926","55.121491","-131.579"CRLF
"99926","55.121491","-131.579"CRLF
"99927","56.307858","-133.37637"CRLF
"99927","56.307858","-133.37637"CRLF
"99929","56.433524","-132.35292"CRLF
"99929","56.433524","-132.35292"CRLF
"99950","55.942471","-133.18479"CRLF
"99950","55.942471","-133.18479"CRLF

Then you can do this to put the values into an array:

Code:
...

<script type="text/javascript">
<!--
// might be just String.fromCharCode(10) for some file systems
var eol = String.fromCharCode(13,10);

// might be empty or a single quote on some files, change delimeter and delimRegEx as per the file
var delimeter = '"';
var delimRegEx = new RegExp("\"","\g"); // global replace, e.g. all in the string
var crRegEx =  new RegExp("\r","\g");   // global replace, e.g. all in the string

var zipArray = new Array();

// even if the file does not have a CR, we can still get rid of any.
var tmpStr = pObjRequest.responseText.replace(crRegEx,'');
var tmpLines = tmpStr.split('\n');

for (var idx=0; idx<tmpLines.length-1; idx++) {

if (delimeter!='') {
tmpLines[idx] = tmpLines[idx].replace(delimRegEx,'\'');
}
//alert(tmpLines[idx]);
zipArray[idx] = new Array();
var tmpArr = tmpLines[idx].split(',');

zipArray[idx] = tmpArr;
}

function showZipArray() {
    var output='';
for (var idx=0; idx<zipArray.length; idx++) {
for (var jdx=0; jdx<zipArray.length; jdx++) {
switch(jdx) {
case 0:
output += 'Zip Code= '+ zipArray[idx][jdx];
break;
case 1:
output += '; Latitude= '+ zipArray[idx][jdx];
break;
case 2:
output += '; Longitude= '+ zipArray[idx][jdx] + '</br>';
break;
}
}
}
return output;
}
//-->
</script>

...

<body>
<div id="content"><script type="text/javascript">document.write(showZipArray());</script></div>
</body>

...


Title: Re: Reading Files With JavaScript
Post by: rdivilbiss on August 22, 2007, 08:05:22 PM
http://www.rodsdot.com/temp_testing/parseCSV.htm (http://www.rodsdot.com/temp_testing/parseCSV.htm)