I'm doing a program that needs to read the source code of a javascript file. I was trying to get the javascript file via XMLHttpRequest and return this.responseText
, however, its value is undefined
.
How do I return the contents of a javascript file in the form of a string (without jQuery or, preferably, without the use of third-party libraries)?
Follow the code below
function getScriptStr(filepath) {
var http = new XMLHttpRequest();
http.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
return this.responseText.split('\n');
}
};
http.open("GET", filepath, true);
http.send();
};