【问题标题】:Script works in Chrome but not in IE or FF脚本在 Chrome 中有效,但在 IE 或 FF 中无效
【发布时间】:2017-04-05 17:20:21
【问题描述】:

我有以下代码在 chrome 中工作,但在 FF 或 IE 中不工作。

该代码允许用户选择一个文本文件并每 10 秒重新读取一次内容,并使用文本文件的内容更新 PRE 标记。

<!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>

有人可以帮助它在其他浏览器中工作吗?

提前致谢。

【问题讨论】:

    标签: javascript google-chrome internet-explorer firefox readfile


    【解决方案1】:

    当一个文件被选中时,输入会有一个当时内容的快照。磁盘上的本地更改不会更新快照。

    Chrome 的实现似乎违反了规范,因此示例仅适用于 Chrome。

    你可以看到另一个问题有解释here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2011-02-22
      • 2012-08-06
      相关资源
      最近更新 更多