respond.proxy.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // http://stackoverflow.com/a/472729
  56. function checkBaseURL(href) {
  57. var el = document.createElement('div'),
  58. escapedURL = href.split('&').join('&amp;').
  59. split('<').join('&lt;').
  60. split('"').join('&quot;');
  61. el.innerHTML = '<a href="' + escapedURL + '">x</a>';
  62. return el.firstChild.href;
  63. }
  64. function checkRedirectURL() {
  65. // IE6 & IE7 don't build out absolute urls in <link /> attributes.
  66. // So respond.proxy.gif remains relative instead of http://example.com/respond.proxy.gif.
  67. // This trickery resolves that issue.
  68. if (~ !redirectURL.indexOf(location.host)) {
  69. var fakeLink = doc.createElement("div");
  70. fakeLink.innerHTML = '<a href="' + redirectURL + '"></a>';
  71. docElem.insertBefore(fakeLink, docElem.firstElementChild || docElem.firstChild );
  72. // Grab the parsed URL from that dummy object
  73. redirectURL = fakeLink.firstChild.href;
  74. // Clean up
  75. fakeLink.parentNode.removeChild(fakeLink);
  76. fakeLink = null;
  77. }
  78. }
  79. function buildUrls(){
  80. var links = doc.getElementsByTagName( "link" );
  81. for( var i = 0, linkl = links.length; i < linkl; i++ ){
  82. var thislink = links[i],
  83. href = links[i].href,
  84. extreg = (/^([a-zA-Z:]*\/\/(www\.)?)/).test( href ),
  85. ext = (baseElem && !extreg) || extreg;
  86. //make sure it's an external stylesheet
  87. if( thislink.rel.indexOf( "stylesheet" ) >= 0 && ext ){
  88. (function( link ){
  89. fakejax( href, function( css ){
  90. link.styleSheet.rawCssText = css;
  91. respond.update();
  92. } );
  93. })( thislink );
  94. }
  95. }
  96. }
  97. if( !respond.mediaQueriesSupported ){
  98. checkRedirectURL();
  99. buildUrls();
  100. }
  101. })( window, document );