Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read var data from text file

Posted on 2013-07-02
4
Medium Priority
?
398 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
[X]
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
  • 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 84

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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

715 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