I have:
var datahere;
var url = "/example.txt"
example.txt has the text "Hello World"
How do I get datahere to equal this string outside of any request?
I'm happy to include PHP into the equations if necessary.
I have:
var datahere;
var url = "/example.txt"
example.txt has the text "Hello World"
How do I get datahere to equal this string outside of any request?
I'm happy to include PHP into the equations if necessary.
Like @J.T. Crowder said, your question is not very specific about your scenario. You can also find plenty of websites explaining in great detail how XMLHttpRequests
work. What you want can be achieved with a simple request like this:
var result, url = "/example.txt";
var xhr = new XMLHttpRequest();
xhr.open("GET",url,true);
xhr.onreadystatechange = function(e) {
if(xhr.readyState == 4 && xhr.status == 200) {
result = xhr.responseText;
document.writeln(result);
}
};
xhr.send();
This specific script will run under any webserver, but it won't run if you open it locally.
Here's one of those websites that explain everything https://xhr.spec.whatwg.org/