Skip to content

Commit

Permalink
Merge pull request #104 from scottjehl/cross-domain
Browse files Browse the repository at this point in the history
Resolves Issue #59: Cross domain CSS files with ? in URL won't load
  • Loading branch information
Scott Jehl committed Mar 16, 2012
2 parents ed8479c + f949305 commit b21c578
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
60 changes: 38 additions & 22 deletions cross-domain/respond-proxy.html
Expand Up @@ -8,13 +8,41 @@
<body> <body>
<script> <script>
(function () { (function () {
var domain, css, url, match, file, ajax, xmlHttp; var domain, css, query, getQueryString, ajax, xmlHttp;


/*
http://stackoverflow.com/questions/4963673/get-url-array-variables-in-javascript-jquery/4963817#4963817
*/
getQueryString = function() {
var ret = {}, parts, i, p;

parts = (document.location.toString().split("?")[1]).split("&");

for (i = 0; i < parts.length; i++) {

p = parts[i].split("=");
// so strings will be correctly parsed:
p[1] = decodeURIComponent(p[1].replace(/\+/g, " "));

if (p[0].search(/\[\]/) >= 0) { // then it"s an array
p[0] = p[0].replace("[]", "");

if (typeof ret[p[0]] != "object") {
ret[p[0]] = [];
}
ret[p[0]].push(p[1]);
} else {
ret[p[0]] = p[1];
}
}
return ret;
};

ajax = function( url, callback ) { ajax = function( url, callback ) {
var req = xmlHttp(); var req = xmlHttp();
if (!req){ if (!req){
return; return;
} }
req.open( "GET", url, true ); req.open( "GET", url, true );
req.onreadystatechange = function () { req.onreadystatechange = function () {
if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){ if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){
Expand All @@ -27,7 +55,7 @@
} }
req.send(); req.send();
}; };

//define ajax obj //define ajax obj
xmlHttp = (function() { xmlHttp = (function() {
var xmlhttpmethod = false, var xmlhttpmethod = false,
Expand All @@ -51,24 +79,12 @@
return xmlhttpmethod; return xmlhttpmethod;
}; };
})(); })();


url = window.location.href; query = getQueryString();

css = query["css"];
if (url) { domain = query["url"];
match = (/css\=(.*\.css)$/).exec(url.slice(url.indexOf('?') + 1));

if (css && domain) {
if (match && match[1]) {
css = match[1];
}

match = (/url\=([^&]+)/).exec(url);

if (match && match[1]) {
domain = match[1];
}
}

if (css) {
ajax(css, function (response) { ajax(css, function (response) {
window.name = response; window.name = response;
window.location.href = domain; window.location.href = domain;
Expand Down
6 changes: 5 additions & 1 deletion cross-domain/respond.proxy.js
Expand Up @@ -7,6 +7,10 @@
urls = [], urls = [],
refNode; refNode;


function encode(url){
return win.encodeURIComponent(url);
}

function fakejax( url, callback ){ function fakejax( url, callback ){


var iframe, var iframe,
Expand All @@ -26,7 +30,7 @@
docElem.insertBefore(iframe, docElem.firstElementChild || docElem.firstChild ); docElem.insertBefore(iframe, docElem.firstElementChild || docElem.firstChild );
} }


iframe.src = checkBaseURL(proxyURL) + "?url=" + redirectURL + "&css=" + checkBaseURL(url); iframe.src = checkBaseURL(proxyURL) + "?url=" + encode(redirectURL) + "&css=" + encode(checkBaseURL(url));


function checkFrameName() { function checkFrameName() {
var cssText; var cssText;
Expand Down

0 comments on commit b21c578

Please sign in to comment.