0

I have the following code that works in chrome however does not work in FF or IE.

The code allows a user to select a text file and re-reads the contents every 10 seconds and updates the PRE tag with the contents of the text file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Read text file every 10 seconds</title>
    <script type="text/javascript">
  
  
 var currentIntervalId = undefined;
  var startOrRestart = function(that) {
  if (currentIntervalId !== undefined) clearInterval(currentIntervalId);
  readText(that); // For executing immediately
  currentIntervalId = setInterval(function() { readText(that); }, 10000);
  };
  
  function readText(that){
   if(that.files && that.files[0]){
   //alert("hello");
    var reader = new FileReader();
    reader.onload = function (e) {
     var contents = e.target.result;//.replace("\r\n","<br/>");
     contents = contents.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
     document.getElementById('board').innerHTML= contents;
    };//end onload()
    reader.readAsText(that.files[0]);
   }//end if html5 filelist support
  } 
  
    </script>
  </head>
  <body>

<input type="file" onchange='startOrRestart(this)' />  <hr />

<pre id="board" contenteditable = "true">
This is where the text from the chosen text file will be loaded.
</pre>

</body>
</html>

Can someone help get this to work in other browsers?

Thanks in advance.

jmcall10
  • 127
  • 1
  • 9

1 Answers1

0

When a file is selected the input has a snapshot of the contents at that point. Local changes on disk don't update the snapshot.

Chrome's implementation appears to break the spec so an example will work only in Chrome.

You can see another question with explanation here

Community
  • 1
  • 1
Alex Slipknot
  • 2,439
  • 1
  • 18
  • 26