Resource data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom de Neef

    Resource data

    I am converting a Pascal app to Javascript.
    The app presents spelling tests. Having completed a test, the user will be
    presented with a random next one.
    Each test is derived from a single line in a txt input file. Each line can
    be considered as a string.
    How can I incorporate this input file in Javascript?
    It would be nice to be able to keep this file (of around 600 lines), since
    it makes additions/changes straightforward . If that is not possible, the
    lines should be integrated into the script in a 'manageable' way.

    TIA
    Tom


  • Stevo

    #2
    Re: Resource data

    Tom de Neef wrote:
    I am converting a Pascal app to Javascript.
    The app presents spelling tests. Having completed a test, the user will be
    presented with a random next one.
    Each test is derived from a single line in a txt input file. Each line can
    be considered as a string.
    How can I incorporate this input file in Javascript?
    It would be nice to be able to keep this file (of around 600 lines), since
    it makes additions/changes straightforward . If that is not possible, the
    lines should be integrated into the script in a 'manageable' way.
    TIA
    Tom
    You could convert your text file into a JS file. That's what I'd do. So
    where your file might look like this now:

    line 1 some text
    line 2 some text
    line 3 some text

    With a quick macro in your favourite text editor, you could turn that into:

    var q=[];
    q[q.length]="line 1 some text";
    q[q.length]="line 2 some text";
    q[q.length]="line 3 some text";

    You still get to leave it as a clear text file, you can still mix up the
    line numbers to change the sequence.

    Just include it with a javascript script tag and the array q is
    available to you. If anything, I would expect it to make your code simpler.

    Comment

    • Evertjan.

      #3
      Re: Resource data

      SAM wrote on 20 feb 2008 in comp.lang.javas cript:
      Tom de Neef a ‚crit :
      >I am converting a Pascal app to Javascript.
      >The app presents spelling tests. Having completed a test, the user
      >will be presented with a random next one.
      >Each test is derived from a single line in a txt input file. Each
      >line can be considered as a string.
      >How can I incorporate this input file in Javascript?
      >It would be nice to be able to keep this file (of around 600 lines),
      >since it makes additions/changes straightforward . If that is not
      >possible, the lines should be integrated into the script in a
      >'manageable' way.
      >
      perhaps can you insert the text file via php or something like that ?
      >
      Or with Ajax-like code?

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • Tom de Neef

        #4
        Re: Resource data


        "Stevo" <please@spam-me.comschreef in bericht
        news:fphc6v$i3d $00$1@news.t-online.com...
        Tom de Neef wrote:
        >I am converting a Pascal app to Javascript.
        >The app presents spelling tests. Having completed a test, the user will
        >be presented with a random next one.
        >Each test is derived from a single line in a txt input file. Each line
        >can be considered as a string.
        >How can I incorporate this input file in Javascript?
        >It would be nice to be able to keep this file (of around 600 lines),
        >since it makes additions/changes straightforward . If that is not
        >possible, the lines should be integrated into the script in a
        >'manageable' way.
        >TIA
        >Tom
        >
        You could convert your text file into a JS file. That's what I'd do. So
        where your file might look like this now:
        >
        line 1 some text
        line 2 some text
        line 3 some text
        >
        With a quick macro in your favourite text editor, you could turn that
        into:
        >
        var q=[];
        q[q.length]="line 1 some text";
        q[q.length]="line 2 some text";
        q[q.length]="line 3 some text";
        >
        You still get to leave it as a clear text file, you can still mix up the
        line numbers to change the sequence.
        >
        Just include it with a javascript script tag and the array q is available
        to you. If anything, I would expect it to make your code simpler.
        Thank you, I have adopted this approach.
        Tom


        Comment

        • Peter Michaux

          #5
          Re: Resource data

          On Feb 20, 6:07 am, Stevo <ple...@spam-me.comwrote:
          Tom de Neef wrote:
          I am converting a Pascal app to Javascript.
          The app presents spelling tests. Having completed a test, the user will be
          presented with a random next one.
          Each test is derived from a single line in a txt input file. Each line can
          be considered as a string.
          How can I incorporate this input file in Javascript?
          It would be nice to be able to keep this file (of around 600 lines), since
          it makes additions/changes straightforward . If that is not possible, the
          lines should be integrated into the script in a 'manageable' way.
          TIA
          Tom
          >
          You could convert your text file into a JS file. That's what I'd do. So
          where your file might look like this now:
          >
          line 1 some text
          line 2 some text
          line 3 some text
          >
          With a quick macro in your favourite text editor, you could turn that into:
          >
          var q=[];
          q[q.length]="line 1 some text";
          q[q.length]="line 2 some text";
          q[q.length]="line 3 some text";
          For faster downloads it could be turned into

          var q=[
          "line 1 some text",
          "line 2 some text",
          "line 3 some text"
          ];

          Note the last text line doesn't have a comma.

          Peter

          Comment

          • SAM

            #6
            Re: Resource data

            Tom de Neef a écrit :
            "SAM" <stephanemoriau x.NoAdmin@wanad oo.fr.invalidsc hreef in bericht
            news:47bc4d18$0 $846$ba4acef3@n ews.orange.fr.. .
            >perhaps can you insert the text file via php or something like that ?
            (snip)
            >
            I appreciate your suggestion, but I think I should master Javascript first,
            before venturing into php or Ajax.
            I don't see JS reading a simple text file (or including it) ...
            At least you'll have to transform the text file in a variable or in a JS
            array.

            file 'testsText.js' :

            var lines = '1st line;2nd line;3rd line';

            or :

            var lines = ['1st line','2nd line','3rd line'];

            or :

            var lines = [
            '1st line',
            '2nd line',
            '3rd line'
            ];

            (that was supposed the php to do)
            (using your original file without to modify it by yourself)
            (no ' will be allowed in any line)(php translated that)
            (after all, perhaps your C script can create this file ?)


            And in the head of your main html page

            <script type="text/javascript" src="testsText. js"></script>
            <script type="text/javascript">
            if(typeof(lines ) == 'string') lines = lines.split(';' );
            function newTest() {
            return lines[Math.floor(Math .random()*lines .length)];
            }
            </script>


            In the body of my idea the call to correction was made server-side and
            the result displayed in a popup to do not use Ajax.

            If you have a file with corrected lines you can load it in same way as
            above and compare the answer with correspondent corrected line
            then display result somewhere in same page without using Ajax.
            But take care you'll force to load 2 files of 600 lines each (which
            weight that will be ?)


            --
            sm

            Comment

            • Tom de Neef

              #7
              Re: Resource data

              "Tom de Neef" <tdeneef@qolor. nlschreef
              >I am converting a Pascal app to Javascript.
              The app presents spelling tests. Having completed a test, the user will be
              presented with a random next one.
              Each test is derived from a single line in a txt input file. Each line can
              be considered as a string.
              How can I incorporate this input file in Javascript?
              It would be nice to be able to keep this file (of around 600 lines), since
              it makes additions/changes straightforward . If that is not possible, the
              lines should be integrated into the script in a 'manageable' way.
              >
              TIA
              Tom
              >
              The answer was given in a later thread: use XMLHttpRequest. The code I now
              use is as follows:

              function loadFile(url) {
              var req = false;
              // branch for native XMLHttpRequest object
              if(window.XMLHt tpRequest && !(window.Active XObject)) {
              try {
              req = new XMLHttpRequest( );
              } catch(e) {
              req = false;
              }
              // branch for IE/Windows ActiveX version
              } else if(window.Activ eXObject) {
              try {
              req = new ActiveXObject(" Msxml2.XMLHTTP" );
              } catch(e) {
              try {
              req = new ActiveXObject(" Microsoft.XMLHT TP");
              } catch(e) {
              req = false;
              }
              }
              }
              if(req) {
              req.open("GET", url, false);
              req.send("");
              return req.responseTex t
              }
              }


              var tests = []
              tests =
              loadFile('ww.op g').split(Strin g.fromCharCode( 13)+String.from CharCode(10))

              Tom


              Comment

              • Evertjan.

                #8
                Re: Resource data

                Thomas 'PointedEars' Lahn wrote on 26 feb 2008 in comp.lang.javas cript:
                tests = tests.split(/\r?\n|\r/);
                >
                tests = tests.split(/[\r\n]/)

                This will skip empty lines,
                which can be either bad or usefull.

                --
                Evertjan.
                The Netherlands.
                (Please change the x'es to dots in my emailaddress)

                Comment

                Working...