Permalink
Browse files

Added a new external cross-domain proxy script for those who need it.…

… Instructions are included in the README file. Huge, Huge thanks to @doctyper, who crafted the majority of the logic behind this workaround. Fixes #28. Fixes #24.
  • Loading branch information...
1 parent 2297fff commit 3500e865e50f492f6091fb01f3c92e76fa32df87 scottjehl committed Jul 19, 2011
View
@@ -28,6 +28,31 @@ Usage Instructions
3. Crack open Internet Explorer and pump fists in delight
+CDN/X-Domain Setup
+======
+
+Respond.js works by requesting a pristine copy of your CSS via AJAX, so if you host your stylesheets on a CDN (or a subdomain), you'll need to upload a proxy page to enable cross-domain communication.
+
+See `cross-domain/example.html` for a demo:
+
+- Upload `cross-domain/respond-proxy.html` to your external domain
+- Upload `cross-domain/respond.proxy.gif` to your origin domain
+- Reference the file(s) via `<link />` element(s):
+
+<pre>
+ &lt;!-- Respond.js proxy on external server --&gt;
+ &lt;link href=&quot;http://externalcdn.com/respond-proxy.html&quot; id=&quot;respond-proxy&quot; rel=&quot;respond-proxy&quot; /&gt;
+
+ &lt;!-- Respond.js redirect location on local server --&gt;
+ &lt;link href=&quot;/path/to/respond.proxy.gif&quot; id=&quot;respond-redirect&quot; rel=&quot;respond-redirect&quot; /&gt;
+
+ &lt;!-- Respond.js proxy script on local server --&gt;
+ &lt;script src="/path/to/respond.proxy.js"&gt;&lt;/script&gt;
+</pre>
+
+Note: HUGE thanks to @doctyper for the contributions in the cross-domain proxy!
+
+
Support & Caveats
======
View
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8" />
+ <title>Respond JS Test Page</title>
+ <link href="http://scottjehl.com/respond-proxy/test.css" rel="stylesheet"/>
+ <link href="http://scottjehl.com/respond-proxy/test2.css" media="screen and (min-width: 600px)" rel="stylesheet"/>
+ <script src="../respond.src.js"></script>
+
+ <!-- Respond.js proxy on external server -->
+ <link href="http://scottjehl.com/respond-proxy/respond-proxy.html" id="respond-proxy" rel="respond-proxy" />
+ <link href="respond.proxy.gif" id="respond-redirect" rel="respond-redirect" />
+ <script src="respond.proxy.js"></script>
+</head>
+<body>
+ <p>This is a visual test file for cross-domain proxy.</p>
+
+ <p>The media queries in the included CSS file simply change the body's background color depending on the browser width. If you see any colors aside from black, then the media queries are working in your browser. You can resize your browser window to see it change on the fly.</p>
+
+
+ <p id="attribute-test">Media-attributes are working too! This should be visible above 600px.</p>
+
+</body>
+</html>
@@ -0,0 +1,80 @@
+<!-- Respond.js: min/max-width media query polyfill. Remote proxy (c) Scott Jehl. MIT Lic. j.mp/respondjs -->
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8" />
+ <title>Respond JS Proxy</title>
+</head>
+<body>
+ <script>
+ (function () {
+ var domain, css, url, match, file, ajax, xmlHttp;
+
+ 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 ){
+ return;
+ }
+ callback( req.responseText );
+ };
+ if ( req.readyState == 4 ){
+ return;
+ }
+ req.send();
+ };
+
+ //define ajax obj
+ xmlHttp = (function() {
+ var xmlhttpmethod = false,
+ attempts = [
+ function(){ return new XMLHttpRequest(); },
+ function(){ return new ActiveXObject("Microsoft.XMLHTTP"); },
+ function(){ return new ActiveXObject("MSXML2.XMLHTTP.3.0"); }
+ ],
+ al = attempts.length;
+
+ while( al-- ){
+ try {
+ xmlhttpmethod = attempts[ al ]();
+ }
+ catch(e) {
+ continue;
+ }
+ break;
+ }
+ return function(){
+ return xmlhttpmethod;
+ };
+ })();
+
+ url = window.location.href;
+
+ if (url) {
+ match = (/css\=(.*\.css)$/).exec(url.slice(url.indexOf('?') + 1));
+
+ if (match && match[1]) {
+ css = match[1];
+ }
+
+ match = (/url\=([^&]+)/).exec(url);
+
+ if (match && match[1]) {
+ domain = match[1];
+ }
+ }
+
+ if (css) {
+ ajax(css, function (response) {
+ window.name = response;
+ window.location.href = domain;
+ });
+ }
+ }());
+ </script>
+</body>
+</html>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,92 @@
+/*! Respond.js: min/max-width media query polyfill. Remote proxy (c) Scott Jehl. MIT Lic. j.mp/respondjs */
+(function(win, doc, undefined){
+ var docElem = doc.documentElement,
+ proxyURL = doc.getElementById("respond-proxy").href,
+ redirectURL = (doc.getElementById("respond-redirect") || location).href,
+ urls = [],
+ refNode;
+
+ function fakejax( url, callback ){
+
+ var iframe,
+ AXO;
+
+ // All hail Google http://j.mp/iKMI19
+ // Behold, an iframe proxy without annoying clicky noises.
+ if ( "ActiveXObject" in win ) {
+ AXO = new ActiveXObject( "htmlfile" );
+ AXO.open();
+ AXO.write( '<iframe id="x"></iframe>' );
+ AXO.close();
+ iframe = AXO.getElementById( "x" );
+ } else {
+ iframe = doc.createElement( "iframe" );
+ iframe.style.cssText = "position:absolute;top:-99em";
+ docElem.insertBefore(iframe, docElem.firstElementChild || docElem.firstChild );
+ }
+
+ iframe.src = proxyURL + "?url=" + redirectURL + "&css=" + url;
+
+ function checkFrameName() {
+ var cssText;
+
+ try {
+ cssText = iframe.contentWindow.name;
+ }
+ catch (e) { }
+
+ if (cssText) {
+ // We've got what we need. Stop the iframe from loading further content.
+ iframe.src = "about:blank";
+ iframe.parentNode.removeChild(iframe);
+ iframe = null;
+
+
+ // Per http://j.mp/kn9EPh, not taking any chances. Flushing the ActiveXObject
+ if (AXO) {
+ AXO = null;
+
+ if (win.CollectGarbage) {
+ win.CollectGarbage();
+ }
+ }
+
+ callback(cssText);
+ }
+ else{
+ win.setTimeout(checkFrameName, 100);
+ }
+ }
+
+ win.setTimeout(checkFrameName, 500);
+ }
+
+
+ function buildUrls(){
+ var links = doc.getElementsByTagName( "link" );
+
+ for( var i = 0, linkl = links.length; i < linkl; i++ ){
+
+ var thislink = links[i],
+ href = links[i].href,
+ ext = /^([a-zA-Z]+?:(\/\/)?(www\.)?)/;
+
+ //make sure it's an external stylesheet
+ if( thislink.rel.indexOf( "stylesheet" ) >= 0 && ext.test( href ) ){
+ (function( link ){
+ fakejax( href, function( css ){
+ link.styleSheet.rawCssText = css;
+ respond.update();
+ } );
+ })( thislink );
+ }
+ }
+
+
+ }
+
+ if( !respond.mediaQueriesSupported ){
+ buildUrls();
+ }
+
+})( window, document );

0 comments on commit 3500e86

Please sign in to comment.