HTML - 从 javascript 中的 URL 位置读取 .txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28828029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:28:50  来源:igfitidea点击:

HTML - read .txt file from URL location in javascript

javascripthtml

提问by Simon

I want to read a .txt file an URL location, lets say from http://www.puzzlers.org/pub/wordlists/pocket.txtand process its content on my page.

我想读取一个 .txt 文件的 URL 位置,比如说来自http://www.puzzlers.org/pub/wordlists/pocket.txt并在我的页面上处理它的内容。

Can you point me out some tutorials or some basic code on how to do this in javascript?

你能指出一些关于如何在 javascript 中执行此操作的教程或一些基本代码吗?

I have a basic HTML code where I have some tables and I want to populate them with text from a .txt from a given URL location, but I do not know how to read the data from that location.

我有一个基本的 HTML 代码,其中有一些表格,我想用来自给定 URL 位置的 .txt 中的文本填充它们,但我不知道如何从该位置读取数据。

<html>
<head>
  <title>Pseudoganglia Interface</title>
  <!-- CSS goes in the document HEAD or added to your external stylesheet -->
  <style type="text/css">
  table.gridtable {
    font-family: verdana,arial,sans-serif;
    font-size:11px;
    color:#333333;
    border-width: 1px;
    border-color: #666666;
    border-collapse: collapse;
  }
  table.gridtable th {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #dedede;
  }
  table.gridtable td {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #ffffff;
  }
  </style>
  <!-- Table goes in the document BODY -->
  <script>
    function getText()
    {
     // read text from URL location
    }
    function populateTables() {
      var tableHP = document.getElementById("tHP");
      // Create an empty <tr> element and add it to the 1st position of the table:
      var row = tableHP.insertRow(tableHP.rows.length);
      // Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);

      // Add some text to the new cells:
      cell1.innerHTML = "NEW CELL1";
      cell2.innerHTML = "NEW CELL2";
    }
  </script>
</head>
<body onload="populateTables()">
<table class="gridtable" id="tHP">
  <tr>
<th colspan=2>HP</th>
  </tr>
  <tr>
<td># SN</td>
<td>% of used RAM</td> 
  </tr>
</table>

<br>

<table class="gridtable" id="tIBM">
  <tr>
<th colspan=2>IBM</th>
  </tr>
  <tr>
<td># CN</td>
<td>% of used RAM</td> 
  </tr>
</table>
</body>
</html>

回答by RomanPerekhrest

this code may help you:

此代码可以帮助您:

function getText(){
    // read text from URL location
    var request = new XMLHttpRequest();
    request.open('GET', 'http://www.puzzlers.org/pub/wordlists/pocket.txt', true);
    request.send(null);
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            var type = request.getResponseHeader('Content-Type');
            if (type.indexOf("text") !== 1) {
                return request.responseText;
            }
        }
    }
}
function populateTables(){
    
    var outer_text = getText();
    outer_text = outer_text.split('\n');    // you can adjust the manner of parsing the received file (regexp)
    
    var tableHP = document.getElementById("tHP");
// Create an empty <tr> element and add it to the 1st position of the table:
    var row = tableHP.insertRow(tableHP.rows.length);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);

// Add some text to the new cells:
    cell1.innerHTML = outer_text[0];
    cell2.innerHTML = outer_text[1];
}