HTML/Javascript - 从在线文件中获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6086083/
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-10-25 19:23:52  来源:igfitidea点击:

HTML/Javascript - Get text from online file

javascripthtml

提问by IsaacL

I have info that Shoutcast outputs as an html file.

我有 Shoutcast 输出为 html 文件的信息。

The html file looks like this: http://216.118.106.247:443/7.html.

html 文件如下所示:http://216.118.106.247:443/7.html.

Is there any way to get the last item in that list/array into Javascript as a string?

有什么方法可以将该列表/数组中的最后一项作为字符串放入 Javascript 中?

I want to output the song info in a html file, I assume that once I get it into JS as a string that I can use the document.write()function to output the code...

我想在html文件中输出歌曲信息,我假设一旦我将它作为字符串输入JS,我就可以使用该document.write()函数输出代码......

Thanks!

谢谢!

回答by Femi

If you look at http://code.google.com/chrome/extensions/xhr.html, you'll need to set up cross-origin requests and then you should be able to use the XMLHttpRequest to fetch the data.

如果您查看http://code.google.com/chrome/extensions/xhr.html,您将需要设置跨域请求,然后您应该能够使用 XMLHttpRequest 来获取数据。

EDITED:

编辑:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = process;
xhr.open("GET", "http://216.118.106.247:443/7.html", true);
xhr.send();

function process()
{
  if (xhr.readyState == 4) {
    var resp = JSON.parse(xhr.responseText);

    // resp now has the text and you can process it.
alert(resp);
  }
}

回答by user3675930

Just write a javascript file (js file) and include with the script tags.

只需编写一个 javascript 文件(js 文件)并包含在脚本标签中。

This file will have your data like that.

该文件将包含您的数据。

<script type="text/javascript" src="data.js"  >

where data.js can be..

data.js 可以在哪里..

var data[];
data[0]="something";

e.t.c

等等

In your page (the one that calls data.js) the array data will be accessible.

在您的页面(调用 data.js 的页面)中,可以访问数组数据。

回答by KZ.

Take a look at XMLHttpRequestaka Ajax requests.

看看XMLHttpRequest又名 Ajax 请求。

There are a ton of libraries that make "Ajax" easy. Try this one:

有大量的库可以让“Ajax”变得简单。试试这个:

http://www.prototypejs.org/api/ajax/request

http://www.prototypejs.org/api/ajax/request

There are limitations with what you can retrieve using ajax. Due to security issues your browser will not let javascript running on yourwebsite.com perform ajax requests to mywebsite.com.

您可以使用 ajax 检索的内容存在限制。由于安全问题,您的浏览器不会让在 yourwebsite.com 上运行的 javascript 向 mywebsite.com 执行 ajax 请求。

Look up cross site scripting.

查找跨站点脚本。

回答by KZ.

There are several methods out there for you to use. But make sure files are in the sameserver or folder.

有几种方法可供您使用。但请确保文件位于同一服务器或文件夹中。

Using XMLHttpRequest: http://www.javascripter.net/faq/xmlhttpr.htm

使用 XMLHttpRequest:http: //www.javascripter.net/faq/xmlhttpr.htm

Using FileSystemObject: http://msdn.microsoft.com/en-us/library/czxefwt8(v=VS.85).aspx

使用 FileSystemObject:http: //msdn.microsoft.com/en-us/library/czxefwt8(v= VS.85).aspx

Using a "helper" Java applet that reads a file or URL for your script

使用“帮助程序”Java 小程序为您的脚本读取文件或 URL

var fileContent='';
var theLocation='';

function readFileViaApplet(n) {
 document.f1.t1.value='Reading in progress...';
 document.ReadURL.readFile(theLocation);
 setTimeout("showFileContent()",100);
}

function showFileContent() {
 if (document.ReadURL.finished==0) {
  setTimeout("showFileContent()",100);
  return;
 }
 fileContent=document.ReadURL.fileContent;
 document.form1.textarea1.value=fileContent;
}

Some other source to reference: http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm(many examples).

其他一些参考来源:http: //www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm(许多示例)。