0

Just like the title says. I need a way to load a JavaScript file via a synchronous ajax request that works in IE 9. The solution cannot use jQuery.

What I have so far (which works in chrome/ff/opera/etc.) is good but gives an "Access Denied" error when used in IE 9. Here is what I have now:

var xhr=new XMLHttpRequest();
xhr.open("GET","//path/to/someJsFile.min.js",false);
xhr.send(null);
var jsText = xhr.responseText;

The XMLHttpRequest object seems to work for some file types, but not .js files. Any ideas? This code is going on a client's page so I have no access to their backend code or the rest of the site's code.

user439299
  • 831
  • 4
  • 12
  • 23
  • IE requires you to use XDomainRequest instead of XHR for cross site. – Subodh Ghulaxe Jan 23 '14 at 16:50
  • 1
    Is this happening while the document is being generated, or some time later. – Kevin B Jan 23 '14 at 16:52
  • read this answer here http://stackoverflow.com/a/5087606/1868660 – Subodh Ghulaxe Jan 23 '14 at 16:53
  • you can use document.write("<\/script>") to sync add scripts during boot. you can hack an iframe to the same effect later, but since IE doesn't iterate window, you need to know the names you need to pluck from iframe.contentWindow. – dandavis Jan 23 '14 at 16:54
  • @Subodh Ghulaxe - XDomainRequest can not be used synchronously can it? – user439299 Jan 23 '14 at 16:54
  • Do you need to load it right at the beginning of the document in the `head` or later when the document s already closed (when the dom is ready?) – t.niese Jan 23 '14 at 16:56
  • 1
    @dandavis and t.niese - document.write is not an option. I need to do it after the document is loaded. Would an iframe be synchronous? I know I can always do just write in a script tag with the JS file as the src but that would be asynchronous and wouldn't work in my case. – user439299 Jan 23 '14 at 16:59
  • from the top page's inertial frame of reference, the iframe would not be sync. the only way i know to pause and fetch like that is hacky. you can use showModalDialog() to pause and return a value from another page. so, you open a simple html shell that says "wait" on the remote that locally ajaxes the file, sets the returnValue with the responseText and calls window.close() to hide the dialog. this works in IE for sure, but chrome might block x-domain returns. – dandavis Jan 23 '14 at 17:09
  • @dandavis - my hacky solution was going to be to do the async version (with XDomainRequest) and then throw a **while(xdr.responseText == '' || i++ < 500000){}** after the async call to basically "wait" for the response (with a 500000 op timeout). This might lock a bunch of other stuff up though... hummmm... – user439299 Jan 23 '14 at 17:18

0 Answers0