Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

This adds support for a cross-domain request to fetch the CSS files #46

Closed
wants to merge 2 commits into from

4 participants

@wklancnik

This allows for a CDN hosted CSS file on a separate domain to work with respond.js for browsers that support it (e.g. IE8). It naturally falls back to an HXR onerror.

Effectively, this adds support for CDNs in IE8, without having to use an iframe.

@scottjehl
Owner

Interesting, thanks I'll take a look. Can you update this to latest on master, btw? Thanks.

@doctyper
Collaborator

Note that XDomainRequest requires you set an Access-Control-Allow-Origin response header on the remote file.

I considered implementing this in my patch (#28) but ultimately decided to limit the amount of setup and just use the iframe proxy. However, this is a great solution if you decide to deprecate IE6 & 7.

@scottjehl
Owner

Mind submitting this one as a separate pull? Then I can grab it real quick. thanks!

@scottjehl
Owner

Hmm. Looks good. Think we could combine this into the existing x-domain plugin (externally)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 20, 2011
  1. @wklancnik
  2. @wklancnik
This page is out of date. Refresh to see the latest.
Showing with 54 additions and 19 deletions.
  1. +54 −19 respond.src.js
View
73 respond.src.js
@@ -23,6 +23,7 @@
head = doc.getElementsByTagName( "head" )[0] || docElem,
links = head.getElementsByTagName( "link" ),
requestQueue = [],
+ isXDomainRequest = win.XDomainRequest,
//loop stylesheets, send text content to translate
ripCSS = function(){
@@ -52,6 +53,14 @@
media: media
} );
}
+ else if (isXDomainRequest){
+ // browser can handle cross-domain CSS lookups
+ requestQueue.push({
+ href: href,
+ media: media,
+ isXDomainRequest: true
+ });
+ }
else{
parsedSheets[ href ] = true;
}
@@ -71,7 +80,7 @@
translate( styles, thisRequest.href, thisRequest.media );
parsedSheets[ thisRequest.href ] = true;
makeRequests();
- } );
+ }, thisRequest.isXDomainRequest );
}
},
@@ -132,7 +141,7 @@
applyMedia();
},
-
+
lastCall,
resizeDefer,
@@ -185,12 +194,12 @@
ss.media = i;
if ( ss.styleSheet ){
- ss.styleSheet.cssText = css;
- }
- else {
+ ss.styleSheet.cssText = css;
+ }
+ else {
ss.appendChild( doc.createTextNode( css ) );
- }
- dFrag.appendChild( ss );
+ }
+ dFrag.appendChild( ss );
appendedEls.push( ss );
}
@@ -198,22 +207,48 @@
head.insertBefore( dFrag, lastLink.nextSibling );
},
//tweaked Ajax functions from Quirksmode
- ajax = function( url, callback ) {
- var req = xmlHttp();
- if (!req){
- return;
- }
- req.open( "GET", url, true );
- req.onreadystatechange = function () {
- if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){
+ ajax = function( url, callback, isxd ) {
+
+ var xhr = function() {
+ var req = xmlHttp();
+
+ if (!req){
return;
+ }
+ req.open( "GET", url, true );
+ req.onreadystatechange = function () {
+ if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){
+ return;
+ }
+ callback( req.responseText );
}
- callback( req.responseText );
+ if ( req.readyState == 4 ){
+ return;
+ }
+ req.send( null );
+
+ };
+
+ if (!!isxd)
+ {
+ // This is an XDomainRequest in IE 8--time to get weird...
+ var xdr = new XDomainRequest();
+
+ if (!xdr){
+ xhr();
+ return;
+ }
+ xdr.open("GET", url);
+ xdr.onerror = xhr;
+ xdr.onload = function () {
+ callback(xdr.responseText);
+ };
+ xdr.send();
}
- if ( req.readyState == 4 ){
- return;
+ else{
+ xhr();
}
- req.send( null );
+
},
//define ajax obj
xmlHttp = (function() {
Something went wrong with that request. Please try again.