Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Read var data from text file

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

789 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