2

I have an <object> tag like this:

<object id="text" data="file.txt"></object>

And I can get the element like this:

var text = document.getElementById("text");

Now, how do I get the content of the text file?

The purpose of this is to:

  1. avoid having to make an XMLHttpRequest;
  2. avoid having text data in the form of a constant in code;
  3. keep a simple plaintext file in it's original form, without JSON or special formatting, to be accessed by code and become a string variable.

In the same fashion you can have an <img> or <audio> tag in the document and then access the image / sound, just for text files.

PS: I'm almost positive the <object> tag is not the ideal tag for this. This is just as far as I came trying to accomplish what I need.

4
  • What are you trying to do, exactly: load data from a file into your web page? Commented Feb 9, 2015 at 22:37
  • More precisely, load a .txt file which is sibling to my .html and .js file into a JS variable. Commented Feb 10, 2015 at 1:27
  • Note that even with the file being added to an object in this fashion, the browser is still going to make a request to the server to get it, so whilst you're avoiding making an explicit request yourself, the browser will still be doing so. Commented Feb 10, 2015 at 11:15
  • Yes, no problem about that. Commented Feb 10, 2015 at 11:41

2 Answers 2

1

You can load your text file within a hidden iframe and access the contents within that:

<html>
    <head>
        <script type="text/javascript">
            function gettext() {
                alert(document.getElementById('ifr').contentDocument.body.innerText);
            }
        </script>
    </head>
    <body>
    <iframe id="ifr" style="display:none" src="test.txt" onload="gettext()"></iframe>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

That's precisely what I wanted! And it works with the <object> tag, too! Thank you!
1

I normally prefer AJAX for something like this. Could you do something like this?

var textContent = '';
var textRequest = new XMLHttpRequest();
textRequest.open('GET','file.txt');

textRequest.onreadystatechange = function() {
    if (textRquest.readystate == 4 && textRequest.status == 200) {
        textContent = textRequest.responseText   
    }
}

textRequest.send();

Is there a reason you have to use an object element?

1 Comment

It doesn't really have to be an object element. All I need is to embed some plain text to my document (preferably from a separate file) so I can read it with JavaScript. I wanted it to be clean and simple and that's why I avoided requests as my first option. Sorry, I should have mentioned that before. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.