How can I get the text inside an object element?

半世苍凉 提交于 2020-01-07 05:03:08

问题


In the object element with the ID x a text file is loaded and displayed correctly. How can I get this text with JavaScript?

I set

y.data = "prova.txt"

then tried

y.innerHTML;
y.text;
y.value;

None of these work.

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <object id="x" data="foo.txt"></object>
    <script>
      var y = document.getElementById("x")
    </script>
  </body>
</html>


回答1:


I'm afraid this isn't going to be easy as you'd like it to be.

According to your comments, you tried AJAX first, but came across CORS problems. This happens when you try to include data from files on a different domain name.

Since that didn't work, you tried to include the file inside an object tag. This works a bit like an iframe - the data will be displayed on the webpage, but for the same reasons as above, you cannot access the data through JavaScript if the file is under a different domain name. This is a security feature. That explains the error you were getting most recently:

Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLObjectElement'


Now, there are a few ways you might be able to get around this.

Firstly, if this is a program exclusively for your own use, you can start your browser with web-security disabled (though this is dangerous for browsing the web generally). In Chrome, for example, you can do this by launching Chrome with the --disable-web-security flag. More details here.

Secondly, you can try to arrange that your document and the file do belong under the same domain. You will probably only be able to do this if you have control of the file.

Your error message (specifically a frame with origin "null") makes me think that you are running the files directly in the web-browser rather than through a server. It might make things work better if you go through an actual server.

If you've got Python installed (it's included on Linux and Mac), the easiest way to do that is to open up the terminal and browse to your code's directory. Then launch a simple Python server:

cd /home/your_user_name/your_directory
python -m SimpleHTTPServer

That will start up a web server which you can access in your browser by navigating to http://localhost:8000/your_file.html.

If you are on Windows and haven't got Python installed, you could also use the built-in IIS server, or WAMP (or just install Python).




回答2:


y.innerHTML = 'Hello World';

will replace everything in the 'x' element with the text 'Hello World', but it looks like you've already loaded another HTML document into the 'x' element. So the question is...

Where exactly in the 'x' element do you want to insert the text? for example 'x' -> html -> body?




回答3:


The object element is loading the text file asynchronously, so if you try to get its data by querying the element, you'll get undefined.

However, you can use the onload attribute in <object> elements.

In your HTML, add an onload that calls a function in your script to catch when the text file has fully loaded.

<object id="x" onload="getData()" data="readme.txt"></object>

In the script, you can get the object's data with contentDocument.

function getData() {
    var textFile = document.getElementById('x').contentDocument;
    /* The <object> element renders a whole 
       HTML structure in which the data is loaded.
       The plain text representation in the DOM is surrounded by <pre> 
       so we need to target <pre> in the <object>'s DOM tree */

    // getElementByTagsName returns an array of matches.
    var textObject = textFile.getElementsByTagName('pre')[0];
    // I'm sure there are far better ways to select the containing element!.

    /*We retrieve the inner HTML from the object*/
    var text = textObject.innerHTML;

    alert(text); //use the content!
}


来源:https://stackoverflow.com/questions/32118167/how-can-i-get-the-text-inside-an-object-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!