开发者

HTML/Javascript - Get text from online file

I have info that Shoutcast outputs as an html file.

The html file looks like this: 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?

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

Thanks!


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.

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);
  }
}


Take a look at XMLHttpRequest aka Ajax requests.

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

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.

Look up cross site scripting.


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

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

Using 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

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).


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

This file will have your data like that.

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

where data.js can be..

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

e.t.c

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜