Skip to content

Commit

Permalink
IE9: Fallback to XDomainRequest if available.
Browse files Browse the repository at this point in the history
On IE9, `XMLHttpRequest` does not allow to do a crossdomain
request. An exception is triggered on `open()` in this case. We
intercept this exception and use `XDomainRequest` if available.

This should close LeaVerou#60.
  • Loading branch information
vincentbernat committed Apr 16, 2012
1 parent 7031ff1 commit 1fcb5f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
27 changes: 21 additions & 6 deletions prefixfree.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ var self = window.StyleFix = {
var url = link.href || link.getAttribute('data-href'),
base = url.replace(/[^\/]+$/, ''),
parent = link.parentNode,
xhr = new XMLHttpRequest();
xhr = new XMLHttpRequest(),
process;

xhr.open('GET', url);

xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
process();
}
}

process = function() {
var css = xhr.responseText;

if(css && link.parentNode) {
Expand Down Expand Up @@ -60,10 +64,21 @@ var self = window.StyleFix = {
parent.insertBefore(style, link);
parent.removeChild(link);
}
}
};

xhr.send(null);

try {
xhr.open('GET', url);
xhr.send(null);
} catch (e) {
// Fallback to XDomainRequest if available
if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.onerror = xhr.onprogress = function() {};
xhr.onload = process;
xhr.open("GET", url);
xhr.send(null);
}
}

link.setAttribute('data-inprogress', '');
},
Expand Down
16 changes: 5 additions & 11 deletions prefixfree.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1fcb5f5

Please sign in to comment.