这是我关于stackoverflow的第一个问题,我希望能得到一些帮助。我尝试用HTML编写PreCompiler。我的意图是替换&gt;&gt; #include /textfile.txt<&lt;使用&gt;&gt;的值textfile.txt&lt;&lt;
我不知道为什么,但我总是得到&#34; undefined&#34;作为被替换的字符串。
<html><head> <SCRIPT type="text/javascript">
function outp(){
//Read String from Textarea
var inputString = document.forms.myForm.InputArea.value;
var replaceWord;
var wordslenght = inputString.split(' ').length;
var word = inputString.split(' ');
for(var i=0; i<wordslenght;i++){
if(word[i]== '#include'){
// File have to be on a server (?)
var quelle = "https://dl.dropboxusercontent.com/u/71761109/test.txt";
var rawFile = new XMLHttpRequest();
rawFile.open("GET", quelle, true);
rawFile.onreadystatechange = function (){
if(rawFile.readyState === 4){
if(rawFile.status === 200 || rawFile.status == 0){
replaceWord= rawFile.responseText;
alert(replaceWord);
}
}
}
rawFile.send(null);
var inputString = inputString.replace(word[i], replaceWord);
}
}
//Write String in OutputArea
document.forms.myForm.OutputArea.value = inputString;
}
</SCRIPT>
<form name="myForm" action="button">
<input type="button" name="compilier" value="GO!" onclick="outp()"></button>
<p>Zu pruefenden Text eingeben:<br>
<textarea name="InputArea" cols="50" rows="10"></textarea>
</p>
<p>Gepruefter Text:<br>
<textarea name="OutputArea" cols="50" rows="10"></textarea>
</p>
</form>
document.forms.myForm.InputArea.value =&#34; #define li,ABC 1 DEF 123.45 #include&#34 ;;