Solved

Read var data from text file

Posted on 2013-07-02
4
392 Views
Last Modified: 2013-07-02
Hi Experts,

I have an HTML page with javascript that processes a large data array. This large data array changes daily so what I would like to do is have my HTML page javascript read the data array from a local text file, something like the following.

<html>
   <head>
      <script>
         <!--#include file="vardata.txt"-->
         ...
         process the data here
         ...
      </script>
   </head>
</html>

The vardata.txt file would be the var data formatted correctly so that simple insertion is all that is needed, for example.

var prices = [
                    [1,2,3,4,5,6,7,8,9,0],
                    [1,2,3,4,5,6,7,8,9,0],
                    [1,2,3,4,5,6,7,8,9,0]
                    ];

The include file directive can not be used as I have shown it above but this shows what I want to do.
0
Comment
Question by:DColin
  • 2
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39292440
javascripts prevent to do file IO operations due to the possibility of security breach.
do u have the html file hosted under iis?
is it a web applciation or a simple standalone html?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39292459
You can only 'include' files that are on the server.
0
 

Author Comment

by:DColin
ID: 39292514
sedgwick'

Yes the html file is hosted on a win server. Is there any way of using asp to insert the var text into the script tags?

DaveBaldwin

The vardata.txt file is on the same server, it is actually in the same folder. The problem is that the include file directive does not work inside script tags.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39292593
if it's hosted then u can use ajax to grab the file and populate the array.
line 14 is where u want to use the txt to populate your array.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">

$(function() {
	 function loadXMLDoc()  {
  var xmlhttp;
  if (window.XMLHttpRequest)  {  
	xmlhttp=new XMLHttpRequest(); 
  } else {  
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  
  }
  xmlhttp.onreadystatechange=function()  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      var txt = xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","1.txt",true);
  xmlhttp.send();
  }

  loadXMLDoc();	
});

</script>

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question