HTML-从javascript中的URL位置读取.txt文件
HTML - read .txt file from URL location in javascript
我想读取一个.txt文件的URL位置,比如说从http://www.puzzlers.org/pub/wordlists/pocket.txt并在我的页面上处理其内容。
你能给我一些关于如何用javascript实现这一点的教程或基本代码吗?
我有一个基本的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>
此代码可能会帮助您:
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];
}
来自codegrepper,使用fetch(IE不支持)。
const url = "http://www.puzzlers.org/pub/wordlists/pocket.txt"
fetch(url)
.then( r => r.text() )
.then( t => //process your text! )
相关文章:
- 使用 javascript 读取.txt文件
- HTML-从javascript中的URL位置读取.txt文件
- 如何用javascript读取.txt文件
- 节点.js读取 txt 文件,同时 c 在其中写入
- 从本地读取txt文件放入数组并将数组放入<选择>
- 从文件夹中读取txt文件的数据,并使用浏览按钮使用javascript填充文本框
- 读取txt文件并解析文本
- 使用JavaScript逐行读取txt文件
- 如何读取.txt文件的第一行
- 读取txt文件到数组+增量变量点击访问下一个数组元素:PHP或Javascript
- 用javascript读取.txt文件并填充数组
- 从jQuery(304未修改)中读取txt文件
- 如何逐行读取txt文件?
- 我的应用程序读取txt文件,即使它们被删除
- 如何使用Javascript(或PHP)读取.txt文件并将内容插入html网页
- 仅用javascript读取TXT文件
- 使用javascript从我的服务器读取txt文件
- googlechrome-使用javascript读取txt文件
- 如何使用Javascript读取.txt文件的内容并将其存储到全局变量中
- 在sharepoint中使用javascript读取TXT文件