I am trying to find a way to import an external .js file and access the response headers from that request.
<script src="external/file.js?onload=callback">
function callback(data) {
data.getAllResponseHeaders();
}
</script>
Unfortunately, this method does not seem to be effective.
Is there a way to retrieve the response header when importing the javascript without making a second request?
Please refrain from using jQuery in your solution.
Thank you for any assistance.
SOLUTION Credit to gaetanoM
oXmlHttp.withCredentials = true; is used for CORS
oXmlHttp.responseType = 'text'; is for DOM input?
Below is the current code I am working with;
<script>
function loadScript(url) {
var oXmlHttp = new XMLHttpRequest();
oXmlHttp.withCredentials = true;
oXmlHttp.responseType = 'text';
oXmlHttp.open('GET', url, true);
oXmlHttp.onload = function () {
if( oXmlHttp.status >= 200 || oXmlHttp.status == XMLHttpRequest.DONE ) {
var x = oXmlHttp.getAllResponseHeaders();
console.log(x);
if(oXmlHttp.responseText !== null) {
var oHead = document.getElementsByTagName('HEAD').item(0);
var oScript = document.createElement("script");
oScript.language = "javascript";
oScript.type = "text/javascript";
oScript.defer = true;
oScript.text = oXmlHttp.responseText;
oHead.appendChild(oScript);
}
}
}
oXmlHttp.send();
}
loadScript("http://url/to/file.js");
</script>