Solved

Read var data from text file

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now