473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Reading text files in javascript - seperating text and function

I have an html web page that displays a box with question and answer fields. I have one JavaScript program that contains all the questions and operational functions. (All learnt in the last 18 months).
The user initially selects a set of questions by a radio button, and setting the initial selection results in the chosen questions being copied into the question and answer arrays.
As more sets of questions are added, I thought it would be better management to have the different sets of questions in text files (obviously on the server) separated from the function code.
I can't quite see how to do this; as clearly, it's not so straight forward, as the JavaScript is downloaded to the users browser - so how can it access a file?
I'm prepared to do the homework, but can you please advise me as to how I might possibly go about this task of separating out the coded questions from the function code.
Oct 6 '10 #1
3 1427
gits
5,390 Expert Mod 4TB
for an idea: you might use an AJAX-call (XMLHttpRequest) for that purpose and then work with the corresponding responseText in you JavaScript code ... there are many ajax-tutorials out there for a start ... and then ask more specific questions when you have such ones regarding this issue ...
Oct 6 '10 #2
rnd me
427 Expert 256MB
just code your questions and answers in javascript.
you can then simply add more scripts to the page to reach the different tests.

something like:

Expand|Select|Wrap|Line Numbers
  1. questions=[
  2.  { text: "what is 5+5",
  3.    offers: [3,10,43,75],
  4.    answer: 1
  5.  }
  6. ]//end questions
  7.  
  8. if(window.popQuestions){
  9.   popQuestions();
  10. }
  11.  
the popQuestions function should be pre-defined from your main page and fill in the html by examining the javascript data. when adding the script, it defines new questions and shows the html automatically; all you have to do is dynamically add the new script tag to link to a new questions file.
Oct 8 '10 #3
gits
5,390 Expert Mod 4TB
yes - that is probably the best solution for a 'database-less' environment. the AJAX method i had in mind would be more a preparation for a later database driven solution - since the data then would just be read from the db ... of course a serverside script could even write a javascript file that could be included like in the mentioned js-file solution above :) ... it seems a bit like a personal preference then ... probably i would prefer the AJAX solution - while the shown 'include a js-file' solution would avoid the XMLHttpRequest and the needed processing of a response - and it seems a bit more straight forward for some people ... especially when they don't have experiences with AJAX and async coding ...
Oct 10 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Thomas Philips | last post by:
In the course of playing around with file input and output, I came across some behavior that is not quite intuitive. I created a simple text file, test.txt, which contains only 3 lines, and which I...
2
by: christos panagiotou | last post by:
hi all I am trying to open some .raw files that represent images (256x256, 8 bit per pixel, no header) in a c++ program I cannot copy paste the module here as it uses a method from the VTK...
18
by: Michael | last post by:
Hi, I moved to c++ from c, and wanted to know what the best way to read data from files is in c++. Any thoughts? fscanf() is possible but fairly painful! Regards Michael
4
by: Roberto Dias | last post by:
Hi all, I've got some column delimited text files used as input file to a power engineering application. I'm trying to create a "more-friendly" interface using C++. The C++ program should be...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
9
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
1
by: BARTKO, Zoltan | last post by:
Hello folks, I have the following problem: I have a bunch of text files, I want to load them into a table where each file will be stored in one row. Question 1: Is there any way how to do...
1
by: svijay | last post by:
hi I have got a strange problem. May I know any solution for this. Here is the detailed description about the problem We have got a mainframe system and also production and development...
2
by: Wes Peters | last post by:
Does anyone know of an article that deals with the subject of reading a structured text file using VBA code in Access? Thanks, Wes
1
by: Trevor17 | last post by:
Hello All, I am currently in a Perl programming class and our assignment was: Create a filehandle with the open function that uses a pipe to list all the files in your current directory and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.