Jay Braun
9/25/2015 5:27:00 PM
Hello. I have read much of the on-line material about the drawbacks of eval(). One of the few cases where eval() seems to be regarded as acceptable is an AJAX case like this, where a script on the server side is being requested:
function evalRequest(url) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", filename);
//perhaps set request header
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
eval(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}
which is exactly the type of case where I am using it. It works great when the requested script is "reasonably" sized, e.g., tens of KB. But occasionally, the server-side application processes so much data that the file might be tens of MB in length. eval() is not only slow (as expected), but does not complete the parsing of the file.
One alternative is to break up the script into smaller pieces when necessary. I would like to avoid that.
The file is not in JSON notation, so I cannot use JSON.parse().
I have read about jQuery.getScript(), but it was not clear to me which parts of the above need to be re-cast. I cannot simply replace eval() with $.getScript(), can I? (I tried this, and the answer is No; a link to a full discussion would be appreciated.)
Perhaps there are some other alternatives. Can a script be "compiled" on the server side, and the resulting binary requested? Or might there be a third-party tool that simply replaces eval() for heavy-duty requirements like this?
Thank you,
Jay