Search code examples
htmltextembedtext-files

Embed text files in html


I would like to display the content of a text file inside a HTML page (.rtf, .txt, .log,...) stored on the server.

I have tried with embed but seems that doesn't work.

<embed src="/path_to_text_file/text.rtf" width="500" height="300">

There is a "simple" method (or tag) to do that or I should scan for the content and print it with, for example, jQuery?


Solution

  • Using a $.ajax() function with a .append() function inside you can easily grab the contents of a text document and display them on the page. Something along the lines of what you see below. Preform this on load of the page to immediately load the file in with the rest of the page.

    $.ajax({
            async:false,
            url: 'folder/file.txt',
            dataType: 'text',
            success: function(data) 
            {
            $('element').append(data);
                }
            });
    

    Play around with it a little bit to get the correct result you are looking for. You could also use PHP but unless you really need to parse the data, PHP is a bit overkill in this situation.