Inserting file into html page using Javascript?

I am currently trying to read text from a file and add it to an element in my html page using DOM and Javascript. I am unable to get the text to format. I tried using innerHtml but didn't create it at all (no line breaks).

Here is the javascript:

http = new XMLHttpRequest();
http.open("GET",FILE,false);
http.send();       
document.getElementById("tbody").innerHTML = http.responseText

      

As I said, the text is appended to the tbody element, but not formatted the way it ever was.


I worked with this code (with pre-tag) but as I said it works on my computer but not on the server which doesn't help.

           http.open("GET",FILE ,false);
           http.send();
           var newtext = document.createTextNode(http.responseText);
           var para = document.getElementById("tbody");
           para.appendChild(newtext);   

      


Here is my javascript code:

getHTTPObject () function {var http = false;

/*@cc_on
    @if (@_jscript_version >= 5)
    try
    {
        http = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            http = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
            http = false;
        }
    }
    @else
    {
        http = false;
    }
@end @*/

if (!http && typeof XMLHttpRequest != 'undefined')
{
    try
    {
        http = new XMLHttpRequest();
    }
    catch (e)
    {
        http = false;
    }
}
return http

      

}

    function loadData()
    {

     http = getHTTPObject();

        if (http)
        {
           http.open("GET","my file name",false);
           http.send();
           var newtext = document.createTextNode(http.responseText);
           var para = document.getElementById("tbody");
           para.appendChild(newtext);   
        }
    }

      

+1


source to share


5 answers


You might be lucky to have the text wrapped in the tag <pre>

. What's in the answer? If it's XML you can use responseXML

.



http = new XMLHttpRequest();
http.open("GET",FILE,false);
http.send();       
document.getElementById("tbody").appendChild(http.responseXML);

      

+1


source


It all depends on what data is in the response. If it is plain text, there is no reason why it should be formatted. If you want html to "respect" whitespace as suggested above, put the text inside the pre-tag.

If the answer is html make sure #tbody is a div or something similar.



You should also learn something like jquery for your DOM manipulation and ajax. This saves you the headaches with cross-browser compatibility and better writing.

+1


source


I would argue that the answer is related to your expression: "It works on my PC, but not on the server." I am guessing that you are in trouble because the file does not exist on the server, or the file path is different. have you verified that the file is being sent to your client by trying the file url in the browser directly?

+1


source


innerHTML accepts (and parses) an html string as well, so the result should be a DOM tree like

(assuming)

<tbody>{responseText}</tbody>

      

But without knowing what type of content is in the text of the answer, it is difficult for you to know the exact cause of your problem.

0


source


The reason this works on your PC but not on the server probably depends on the timing. AJAX is asynchronous, but you think of it as synchronous.

0


source







All Articles