HowTo use JavaScript to read in external file and assign datastream to variable for use with other functions
The jQuery.get works as expected and well documented to push the stream to target divElement.
GOAL :: Does anyone know a way to populate the open 'data-read' stream
NOT into an divElement but INTO a <$variable> for other functions to use the passed-in data ?
in other words : How can I use JavaScript to read in external file and assign the data-stream to a local session variable for use with other funtions during the Client side experience...
thanks so much
note: I cannot default to using a scripting language like PHP or ASP I need another workaround...
...<script language="JavaScript">//GOAL :: I want to load the contents into a variable as a sting//jQuery.get( url, [ data ], [ success(data, textStatus, jqXHR) ], [ dataType ] )// ref. http://api.jquery.com/jQuery.getjQuery.get('myTextFile.log', function(data) {//Alert the results from requesting filealert(data);//the following line will insert the var data (raw log) in the divElement$('#readMyFile').html(data)}); </script>...<div id="readMyFile"></script>...
Use an synchronous ajax call : $.ajax({"url":"myTextFile.log","async":false}).responseText
<script language="JavaScript">var myString; // a global var to share it with all your function...myString = $.ajax({"url":"myTextFile.log","async":false}).responseText;...// I'm not sure you still want the following ://alert(myString);//$('#readMyFile').html(myString)</script>...<div id="readMyFile"></script>...
ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.
One of a set of tools we're offering as a way to say thank you for being a part of the community.
ok I pullled everything out of my code working JUST with this... I renamed my log file to myTextFile.log and I still get nothing in FireFox works in IE ONLY !!! agggh!
@leakim97 not working... (course I did correct closing </div> at end of sample code)
validator says theres a syntax error with ::
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script><script language="javascript" type="text/javascript"> var myString; // a global var to share it with all your function $(document).ready(function () { $("input").click(function() { myString = $.ajax({"url":"myTextFile.log","async":false}).responseText; showIt(); }); }); function showIt() { $("#readMyFile").html(myString.replace(/\n/g,"<br />")); }</script></head><body><input type="button" value="click" /><br /><div id="readMyFile"></div></body></html>
I need to take the "onclick event" out of the equation on your examples
I need to streamline it to populate the Array scraping the external file and return the results of the Array .. you can check the streamed value is neccessary as you step thru it...
but most importantly I cannot seem to populate the array
I can verify the stream is read by the onclick event but
I cannot verify the passing of that value into the array for use with
body onload function... thx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script><script language="javascript" type="text/javascript"> var myString, myArray; // a global var to share it with all your function $(document).ready(function () { myString = $.ajax({"url":"myTextFile.log","async":false}).responseText; showIt(); }); function showIt() { myArray = myString.split(",");alert("myArray have : " + myArray.length + " elements"); }</script></head><body></body></html>
postid_34898869 Proof of concept cant get to work right : http://www.widener.edu/webdev/expertexchange/Q_26822624/postid_34898869.htm
.. but this doesnt matter to figure out...
..................................................................................................................................
... this works in IE at least here in form Im working on ... http://www.widener.edu/webdev/expertexchange/Q_26822624/index.htm
now I just need the external file values to be populated into the array WUindex[]
cannot get the logic/syntax to work for line 22
in order to function for line 3358
guess I cannot introduce an addEventListener because I learned its not IE supported... the read-in file script already doesnt work with firefox so I can't worry about cross compatibility must stay with IE supported DOM methods
@Proculopsis thanks man but that only does a part. what I also need is Im struggling to get the value read in from the stream to also be populated in an array ... split isnt seeming to work ...
>>I tried to download your file directly with the browser :
@leakim971 surprised not getting that halted studying the code...
you could reference that file from any where http://absolute path ...
what it was is that I learned myTextFile.log cannot be used via IIS default I had to just now make it a .txt file changing the extension
thanks guys for looking into this more...
Im just struggling trying to get that array 'WUArray' 's cells populated I just have it returning 'undefined' :(
http://www.widener.edu/webdev/expertexchange/Q_26822624/index3.htm
(clicking the University lookup button doesnt work) not just a "display test"
can you help me figure out why I cant harness the values in the Array
I am trying to do that on line2842
like I can do if I just hardcode in cell values like in lines 87-90
var EXindex=new Array('#Exton','#test','#inline','#hardcoded');// type E to test
var DEindex=new Array('#Delaware','#test','#inline','#hardcoded');// type D to test
var CHindex=new Array('#Chester','#test','#inline','#hardcoded'); // type C to test
var HBindex=new Array('#Harrisburg','#test','#inline','#hardcoded');// type H to test
thanks for all your guys' help were close...
Since:
line 2895 : works CHSearchbase(document.getElementById("qCH"),CHindex);
for line 89: var CHindex=new Array('#Chester','#test','#inline','#hardcoded'); // type C to test
why cant I get
line 2885 : WUSearchbase(document.getElementById("qWU"),WUArray);
to read for our programming we are scraping the array from the external file ?
should it be something more akin to :
WUSearchbase(document.getElementById("qWU"), JSON.stringify( WUArray ) );
?
thanks
I am w/command line 2895 : CHSearchbase(document.getElementById("qCH"),CHindex);
I am using the same working code to 'drive' the hard-coded array CHindex[] lines 760 thru 1266
as I am also w/command line 2904 : HBSearchbase(document.getElementById("HB"),HBindex);
I am using the same working code to 'drive' the hard-coded array HBindex[] lines 1271 thru 1775
as I am also w/command line 2912 : EXSearchbase(document.getElementById("qEX"),EXindex);
I am using the same working code to 'drive' the hard-coded array EXindex[] lines 1780 thru 2284
as I am also w/command line 2921 : DESearchbase(document.getElementById("qDE"),DEindex);
I am using the same working code to 'drive' the hard-coded array DEindex[] lines 2289 thru 2793
see lines 87 thru 90 for population of these hard-coded arrays...
WUArray is an array or a table ? It is suppose to be an array
what a 'table' is more like a hashmap ?
I want to mimic and populate hardcoded cells w/ values like in lines 87-90
var EXindex=new Array('#Exton','#test','#inline','#hardcoded');// type E to test
var DEindex=new Array('#Delaware','#test','#inline','#hardcoded');// type D to test
var CHindex=new Array('#Chester','#test','#inline','#hardcoded'); // type C to test
var HBindex=new Array('#Harrisburg','#test','#inline','#hardcoded');// type H to test
I am trying to get to populate the WUArray from the external file university.txt
...split same format, able to be handled by other functions the same way EXindex, DEindex, CHindex, & HBindex can be just no matter what I do it comes back to me undefined... :(
@leakim971
ok clipboard 04 is me trying to printout the Array but its still undefined.
thanks for showing me this but its all null Im needing to figure out how to NOT have WUArray undefined.
you mentioned earlier post ID:34909352
>>If WUArray is a json object you should modify WUSearchbase (or create WUSearchbaseJSON) to handle it
should I make it one ? all thats new to me... thoughts on that ?
Im looking to fix how to make the Array NOT undefined how do we remedy ?
thx
>>or inside the same tag
not sure what tag is... does it mean
<script type="text/javascript"> ???
>>Please show me where you populate it.
isnt it http://www.widener.edu/webdev/expertexchange/Q_26822624/index3.htm :: line 49?
function getContent( data, status, response ) {
WUArray = response.responseText.split( /,/ );
}
?? doesnt that perform the same as line 87 :: var EXindex=new Array('#Exton','#test','#inline','#hardcoded'); ??
>>Confirm it's a global variable.
Im going to try to learn how to be sure it is any suggestion how to confirm this ?
>not sure what tag is... does it mean <script type="text/javascript"> ???
Yes
>>Please show me where you populate it.
OK. What about my code in my first post ?
jQuery(document).ready( function () {
WUArray = $.ajax({"url":"http://www.widener.edu/webdev/expertexchange/Q_26822624/university.txt","async":false}).responseText;
alert(WUArray); // please post what you get with this
WUArray = WUArray.split(",")
$("#click-me").click( display );
});
>>Confirm it's a global variable.
It is declared as global line 40, don't worry :) !!!
ok Im putting all methods/ functions in same tag...
//// @ Author:leakim971 Ive been toggling testing between this and @Proculopsis suggestions
///////////////////////////////////////////////////////////////////////////////////////////
jQuery(document).ready( function () {
WUArray = $.ajax({"url":"http://www.widener.edu/webdev/expertexchange/Q_26822624/university.txt","async":false}).responseText;
alert(WUArray); // please post what you get with this
WUArray = WUArray.split(",")
$("#click-me").click( display );
////////////////////////////////////////////////////////////////////////////////////////////
});/// NOTE THIS jQuery DOES NOT seem to work in IE but in FFX it does...
/// see http://www.widener.edu/webdev/expertexchange/Q_26822624/index3.htm
///////////////////////////////////////////////////////////////////////////////////////////
//should I read it in as a string first as earlier suggested then populate a Array?
var theUniversityIndex = ""; // a new String
var WUArray; // global Array variable
theUniversityIndex = $.ajax({"url":"university.txt","async":false}).responseText;
//THEN split to an array ...
WUArray = theUniversityPhoneIndex.split(",")
// or straight to an array as last post suggests WUArray = WUArray.split(",") ??????????
I put in 'debugger' but that doesnt seem to do anything ?...
your statement having me nest all javascript in same tag got me thinking...
Im wondering if I should pursue more of a JSON object so I may be able to pass the array between functions sort of like http://www.widener.edu/webdev/expertexchange/Q_26822624/index5.htm is doing in code where Id nest all functions in one BIG this.load function ....
ok I think weve gone round and round with no solution so far looking at external webpages so I figured Id send the working code with hardcoded arrays inline for the ticket ...
Do you mean something like this, where variable theText is accessible to the rest of the dom:
Open in new window