Resource data

T

Tom de Neef

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
 
S

Stevo

Tom said:
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.
 
S

SAM

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 ?

<script type="text/javascript">

// JS array of each line
var q = [
?<php
$fo = 'test_texts.txt';
$lines = file( $fo );
$last_line = count($lines)-1;
foreach($lines as $line_num => $line) {
echo "'".htmlspecialchars(trim($line),ENT_QUOTES)."'";
if($line_num < $last_line) echo ",\n";
}
?>
];

function newTest() {
var line_num = Math.floor(Math.random()*q.length);
document.getElementById('test').innerHTML= q[line_num];
document.form1.test_text.value = q[line_num];
document.form1.line_num.value = line_num;
}
function pop() {
if(typeof(popup) == 'undefined' || popup.closed)
popup=window.open('','popup','width=500,height=400,resizable=1');
popup.focus();
}
window.onload = newTest;
</script>
<h2>Test : <span id="test"><span></h2>
<form name="form1" action="correct_test.php" target="popup"
onsubmit="pop();">
<input type="hidden" name="line_num">
<input type="hidden" name="test_text">
<p>Your answer : <input name="answer">
<input type="submit" onclick="setTimeout('newTest()',500);"
value="Evaluate">
</form>
 
E

Evertjan.

SAM wrote on 20 feb 2008 in comp.lang.javascript:
Tom de Neef a ‚crit :

perhaps can you insert the text file via php or something like that ?

Or with Ajax-like code?
 
T

Tom de Neef

SAM said:
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 ?

<script type="text/javascript">

// JS array of each line
var q = [
?<php
$fo = 'test_texts.txt';
$lines = file( $fo );
$last_line = count($lines)-1;
foreach($lines as $line_num => $line) {
echo "'".htmlspecialchars(trim($line),ENT_QUOTES)."'";
if($line_num < $last_line) echo ",\n";
}
?>
];

function newTest() {
var line_num = Math.floor(Math.random()*q.length);
document.getElementById('test').innerHTML= q[line_num];
document.form1.test_text.value = q[line_num];
document.form1.line_num.value = line_num;
}
function pop() {
if(typeof(popup) == 'undefined' || popup.closed)
popup=window.open('','popup','width=500,height=400,resizable=1');
popup.focus();
}
window.onload = newTest;
</script>
<h2>Test : <span id="test"><span></h2>
<form name="form1" action="correct_test.php" target="popup"
onsubmit="pop();">
<input type="hidden" name="line_num">
<input type="hidden" name="test_text">
<p>Your answer : <input name="answer">
<input type="submit" onclick="setTimeout('newTest()',500);"
value="Evaluate">
</form>

I appreciate your suggestion, but I think I should master Javascript first,
before venturing into php or Ajax.
Tom
 
T

Tom de Neef

Stevo said:
Tom said:
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
 
P

Peter Michaux

Tom said:
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
 
S

SAM

Tom de Neef a écrit :
(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 ?)
 
T

Tom de Neef

Tom de Neef said:
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.XMLHttpRequest && !(window.ActiveXObject)) {
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
// branch for IE/Windows ActiveX version
} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
if(req) {
req.open("GET", url, false);
req.send("");
return req.responseText
}
}


var tests = []
tests =
loadFile('ww.opg').split(String.fromCharCode(13)+String.fromCharCode(10))

Tom
 
T

Thomas 'PointedEars' Lahn

Tom said:
var tests = []

The initialization does not make sense, as you throw the array object away
afterwards:
tests =
loadFile('ww.opg').split(String.fromCharCode(13)+String.fromCharCode(10))

var tests = loadFile('ww.opg');
if (tests)
{
// maybe tests.split("\r\n") suffices
tests = tests.split(/\r?\n|\r/);
}
else
{
// handle error
}


PointedEars
 
E

Evertjan.

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

tests = tests.split(/[\r\n]/)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top