well, getfile can be used to get files from SYSTEM but, how do I download site/t.txt ?
I couldnt really find it, and what I found is not useful, please redirect me if asked before.
well, getfile can be used to get files from SYSTEM but, how do I download site./t.txt ?
I couldnt really find it, and what I found is not useful, please redirect me if asked before.
Share Improve this question asked Aug 4, 2013 at 17:24 purplecirclespurplecircles 6232 gold badges7 silver badges8 bronze badges 6- How about a GET request with ajax? – MightyPork Commented Aug 4, 2013 at 17:26
- 2 You don't. Javascript has a same origin policy! – adeneo Commented Aug 4, 2013 at 17:27
- I think you need a server side script for that. – putvande Commented Aug 4, 2013 at 17:36
- @adeneo - And it has ways around it, notably CORS. – Quentin Commented Aug 4, 2013 at 18:33
- @Quentin - more notably JSONP, but you can't just get a random file like site./t.txt unless one or the other is supported by site. – adeneo Commented Aug 4, 2013 at 18:40
2 Answers
Reset to default 4What your asking to do is pretty straight forward with in modern browsers with an XMLHTTPRequest. For example:
function load(url, callback) {
var xhr = new XMLHTTPRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) callback(xhr.responseText);
};
xhr.open("GET", url, true);
}
load("site./t.txt", function (contents) {
// contents is now set to the contents of "site./t.txt"
});
But to ensure plete browser patibility with Internet Explorer a little more code is required since Internet Explorer uses the ActiveXObject instead of XMLHTTPRequest.
function createXHR() {
if (typeof XMLHTTPRequest === "undefined") {
if (createXHR._version) return new ActiveXobject(createXHR._version);
else {
var versions = [
"Micrsoft.XMLHTTP",
"Msxml2.XMLHTTP",
"Msxml2.XMLHTTP",
"Msxml2.XMLHTTP.3.0",
"Msxml2.XMLHTTP.4.0",
"Msxml2.XMLHTTP.5.0",
"Msxml2.XMLHTTP.6.0"
];
var i = versions.length;
while (--i) try {
var v = versions[i], xhr = new ActiveXObject(v);
createXHR._version = v;
return xhr;
} catch {}
}
} else return new XMLHTTPRequest();
}
function load(url, callback) {
var xhr = createXHR();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) callback(xhr.responseText);
};
xhr.open("GET", url, true);
}
I would really remend using a library such as jQuery instead of this. For more information
- XMLHTTPRequest API reference
- Quirksmode cross browser XMLHTTPRequest
As long as you are not working against the Same Origin Policy, this is quite easy. In this case, the domains match if your script is embedded in a page from foo. and requesting a file foo./* and not subdomain.foo./*.
You just need to make a GET request with XMLHttpRequest
for the file and read the file's contents from the response.
If the file is on foo. but the page isn't, you'll need to host the script on foo. and then include it in this page with <script src="foo./filerequestscript.js"></script>
. (Of course, if you don't control foo., that's not likely to happen.)