How can I access plain texts loaded in <iframe> in IE?

I have <iframe>one whose srcpoint is to a plain text file (not HTML). The text is loaded and displayed on the screen, but seems to be hidden from JavaScript.

Other browsers are iframe.contentWindow.document.body.innerTextenough to get it for you, but IE returns an empty string in this case.

Is there a way that IE can access text inside a file without server involvement?

+3
source share
4 answers

You can read this file using XmlHttpRequest. If the browser can read it, then XmlHttpRequest.

/* Read a file  using xmlhttprequest 

If the HTML file with your javascript app has been saved to disk, 
this is an easy way to read in a data file.  Writing out is 
more complicated and requires either an ActiveX object (IE) 
or XPCOM (Mozilla).

fname - relative path to the file
callback - function to call with file text
*/
function readFileHttp(fname, callback) {
   xmlhttp = getXmlHttp();
   xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState==4) { 
          callback(xmlhttp.responseText); 
      }
   }
   xmlhttp.open("GET", fname, true);
   xmlhttp.send(null);
}

/*
Return a cross-browser xmlhttp request object
*/
function getXmlHttp() {
   if (window.XMLHttpRequest) {
      xmlhttp=new XMLHttpRequest();
   } else if (window.ActiveXObject) {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   if (xmlhttp == null) {
      alert("Your browser does not support XMLHTTP.");
   }
   return xmlhttp;
}

readFileHttp(fname, callback), iframe.src fname. , , , .

- :

var myIFrame = document.getElementById('iframeIdGoesHere');
readFileHttp(myIFrame.src, function(result){
    //process the result
});
+1

, innerHTML?

, "" - XMLHttpRequest.

0

EDIT :

, body js . :

iframe.contentWindow.document.getElementsByTagName('body')[0].innerText 
  • ( - ):

    , <input type='file' />. , , . ( , <input type='file' /> ...)

0

window.frames[0].document.getElementsByTagName("body")[0].innerHTML

?

0

Source: https://habr.com/ru/post/1761254/


All Articles