Read IFrame content using JavaScript

后端 未结 4 1223
误落风尘
误落风尘 2020-12-10 19:37

Ok, This is my first time dealing seriously with IFrames and I cant seem to understand a few things:

First the sample code I am testing with:



        
相关标签:
4条回答
  • 2020-12-10 19:48

    That's how browsers treat text files, because they 'look better' this way (not only inside iframe). Browsers can process lot's of file types, and it's unreasonable to expect them to show everything in raw form, right? Because browser pages (and iframes) are about presentation, nobody really uses iframes for configuration or to read raw data from the disk.

    If you want to have full control over presentation, just change file type to html and it will be treated like html. (in particular, it will solve 'pre' problem)

    Will this answer your questions?

    0 讨论(0)
  • 2020-12-10 19:50

    1) The iframe seems to be behaving as a HTML document and the file text is actually part of the body instead. Why ?

    you’re using the DOM/JS interface. this will only work, if the content is treated as HTML/XML.

    0 讨论(0)
  • 2020-12-10 19:54

    innerHTML does not return the exact content of an element, its a non-standardized method that returns HTML which is equivalent to the real content, and in HTML the equivalent to plain text is <pre>foo...</pre>.

    You might have better luck with the innerText property..

    0 讨论(0)
  • 2020-12-10 20:03

    I was having a hard time getting the contents of a TXT file that was the src of an iframe. This is my solution:

    document.getElementById( 'iframeID' ).contentWindow.document.body.innerText
    
    0 讨论(0)
提交回复
热议问题