respond.proxy.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*! Respond.js: min/max-width media query polyfill. Remote proxy (c) Scott Jehl. MIT/GPLv2 Lic. j.mp/respondjs */
  2. (function(win, doc, undefined){
  3. var docElem = doc.documentElement,
  4. proxyURL = doc.getElementById("respond-proxy").href,
  5. redirectURL = (doc.getElementById("respond-redirect") || location).href,
  6. baseElem = doc.getElementsByTagName("base")[0],
  7. urls = [],
  8. refNode;
  9. function encode(url){
  10. return win.encodeURIComponent(url);
  11. }
  12. function fakejax( url, callback ){
  13. var iframe,
  14. AXO;
  15. // All hail Google http://j.mp/iKMI19
  16. // Behold, an iframe proxy without annoying clicky noises.
  17. if ( "ActiveXObject" in win ) {
  18. AXO = new ActiveXObject( "htmlfile" );
  19. AXO.open();
  20. AXO.write( '<iframe id="x"></iframe>' );
  21. AXO.close();
  22. iframe = AXO.getElementById( "x" );
  23. } else {
  24. iframe = doc.createElement( "iframe" );
  25. iframe.style.cssText = "position:absolute;top:-99em";
  26. docElem.insertBefore(iframe, docElem.firstElementChild || docElem.firstChild );
  27. }
  28. iframe.src = checkBaseURL(proxyURL) + "?url=" + encode(redirectURL) + "&css=" + encode(checkBaseURL(url));
  29. function checkFrameName() {
  30. var cssText;
  31. try {
  32. cssText = iframe.contentWindow.name;
  33. }
  34. catch (e) { }
  35. if (cssText) {
  36. // We've got what we need. Stop the iframe from loading further content.
  37. iframe.src = "about:blank";
  38. iframe.parentNode.removeChild(iframe);
  39. iframe = null;
  40. // Per http://j.mp/kn9EPh, not taking any chances. Flushing the ActiveXObject
  41. if (AXO) {
  42. AXO = null;
  43. if (win.CollectGarbage) {
  44. win.CollectGarbage();
  45. }
  46. }
  47. callback(cssText);
  48. }
  49. else{
  50. win.setTimeout(checkFrameName, 100);
  51. }
  52. }
  53. win.setTimeout(checkFrameName, 500);
  54. }
  55. function checkBaseURL(href) {
  56. if (baseElem && href.indexOf(baseElem.href) === -1) {
  57. bref = (/\/$/).test(baseElem.href) ? baseElem.href : (baseElem.href + "/");
  58. href = bref + href;
  59. }
  60. return href;
  61. }
  62. function checkRedirectURL() {
  63. // IE6 & IE7 don't build out absolute urls in <link /> attributes.
  64. // So respond.proxy.gif remains relative instead of http://example.com/respond.proxy.gif.
  65. // This trickery resolves that issue.
  66. if (~ !redirectURL.indexOf(location.host)) {
  67. var fakeLink = doc.createElement("div");
  68. fakeLink.innerHTML = '<a href="' + redirectURL + '"></a>';
  69. docElem.insertBefore(fakeLink, docElem.firstElementChild || docElem.firstChild );
  70. // Grab the parsed URL from that dummy object
  71. redirectURL = fakeLink.firstChild.href;
  72. // Clean up
  73. fakeLink.parentNode.removeChild(fakeLink);
  74. fakeLink = null;
  75. }
  76. }
  77. function buildUrls(){
  78. var links = doc.getElementsByTagName( "link" );
  79. for( var i = 0, linkl = links.length; i < linkl; i++ ){
  80. var thislink = links[i],
  81. href = links[i].href,
  82. extreg = (/^([a-zA-Z:]*\/\/(www\.)?)/).test( href ),
  83. ext = (baseElem && !extreg) || extreg;
  84. //make sure it's an external stylesheet
  85. if( thislink.rel.indexOf( "stylesheet" ) >= 0 && ext ){
  86. (function( link ){
  87. fakejax( href, function( css ){
  88. link.styleSheet.rawCssText = css;
  89. respond.update();
  90. } );
  91. })( thislink );
  92. }
  93. }
  94. }
  95. if( !respond.mediaQueriesSupported ){
  96. checkRedirectURL();
  97. buildUrls();
  98. }
  99. })( window, document );