Navigate
Home
ArticleWiki
Forum
Newsletter
Links
Tech News
Welcome Guest.
Username:

Password:

Remember me

Reading Files With JavaScript
Welcome, Guest. Please login or register.
February 05, 2012, 04:59:46 AM
11513 Posts in 1262 Topics by 496 Members
Latest Member: Beerdernill
Experts Round Table Network  |  Clientside Technology  |  Javascript  |  Reading Files With JavaScript « previous next »
Pages: [1]
Author Topic: Reading Files With JavaScript  (Read 3564 times)
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« 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 was throwing errors in older AppleWebKit engine browsers.

I recently treaked the coded at: 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
Logged

Rod
GrandSchtroumpf
Mentor

Offline Offline

Posts: 432



« Reply #1 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".
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #2 on: June 21, 2007, 10:27:54 AM »

There are problems with it for sure.
Logged

Rod
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #3 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.
Logged

Rod
morchuboo

Offline Offline

Posts: 1


« Reply #4 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
Logged
GrandSchtroumpf
Mentor

Offline Offline

Posts: 432



« Reply #5 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
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #6 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>

...
Logged

Rod
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #7 on: August 22, 2007, 08:05:22 PM »

http://www.rodsdot.com/temp_testing/parseCSV.htm
Logged

Rod
Pages: [1]
« previous next »
    Jump to: