0

This is my first Question on stackoverflow and i hope to get here some help. I try to write an PreCompiler in HTML. My intetion is to replace >> #include /textfile.txt << with the value of the >> textfile.txt << .

I don't know why, but i always get "undefined" as the replaced String.

<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 = "#define li , ABC 1 DEF 123.45 #include";

Sky
  • 27
  • 3
  • Ajax is asynchronous, there is a reason why you're using a `onreadystatechange` callback! You can't use the `responseText` until the Ajax call has been completed, assigning it to a variable inside of the asynchronous callback won't magically make the response available in the synchronous part. See the second answer on [How to return the response from an AJAX call?](http://stackoverflow.com/q/14220321/1331430) – Fabrício Matté Apr 09 '14 at 20:15
  • thank you @FabrícioMatté, now i know my real problem ^^ – Sky Apr 09 '14 at 20:22

0 Answers0