I know this has been visited a lot, and I have managed to get this working with other projects. I have inherited this chunk of code that does not work in IE:
function loadTemplate(template, alt_cache_name) {
if (templates[template] !== undefined) {
if (alt_cache_name !== undefined) {
templates[alt_cache_name] = templates[template];
}
return templates[template];
} else {
return $.get(template, function (response) {
if (alt_cache_name !== undefined) {
templates[alt_cache_name] = response;
}
templates[template] = response;
}, 'text');
}
}
The issue is with the $.get().
The value of template
is a url to a specific html template.
I tried the following, but I don't think the return type is the same as what was returned from $.get():
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
function loadTemplate(template, alt_cache_name) {
if (templates[template] !== undefined) {
if (alt_cache_name !== undefined) {
templates[alt_cache_name] = templates[template];
}
return templates[template];
} else {
var request = createCORSRequest("GET", template);
var content = "";
if (request) {
request.onload = function () {
//do something with request.responseText
console.log(request.responseText);
if (alt_cache_name !== undefined) {
templates[alt_cache_name] = response.responseText;
}
templates[template] = response.responseText;
};
}
return request;
// return $.get(template, function (response) {
// if (alt_cache_name !== undefined) {
// templates[alt_cache_name] = response;
// }
// templates[template] = response;
// }, 'text');
}
}
I even tried the $.get
with:
jQuery.support.cors = true;
Can someone shed some light on how to get this working?