Solved

Read var data from text file

Posted on 2013-07-02
4
389 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now