Monday, July 21, 2008

Reading a txt file Content from URL by JavaScript

The html file having this script should be in the same domain with txt file for accessing from mozilla firefox because of security reason. From internet explorer txt file from any url can be read.

var xmlhttp;
function getFile(pURL) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",pURL,true);
xmlhttp.onreadystatechange = showFileContent;
xmlhttp.send(null);
}

function showFileContent() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
var out=xmlhttp.responseText;
document.getElementById('theExample').innerHTML=out;
}
}
}

See my codes, 1 having this script and reads contents from 2.
  1. http://debasisroy144.googlepages.com/helloworld.html
  2. http://debasisroy144.googlepages.com/helloworld.txt

1 comment:

Facebook Pretender said...

change the links man

now they are on appspot...