Reading text files in javascript - seperating text and function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amicodavid
    New Member
    • Oct 2010
    • 1

    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.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    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 ...

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      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:

      Code:
      questions=[
       { text: "what is 5+5",
         offers: [3,10,43,75],
         answer: 1
       }
      ]//end questions
      
      if(window.popQuestions){
        popQuestions();
      }
      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.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        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 ...

        Comment

        Working...