matchmedia.polyfill.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas. Dual MIT/BSD license */
  2. /*! NOTE: If you're already including a window.matchMedia polyfill via Modernizr or otherwise, you don't need this part */
  3. (function(w){
  4. "use strict";
  5. w.matchMedia = w.matchMedia || (function( doc, undefined ) {
  6. var bool,
  7. docElem = doc.documentElement,
  8. refNode = docElem.firstElementChild || docElem.firstChild,
  9. // fakeBody required for <FF4 when executed in <head>
  10. fakeBody = doc.createElement( "body" ),
  11. div = doc.createElement( "div" );
  12. div.id = "mq-test-1";
  13. div.style.cssText = "position:absolute;top:-100em";
  14. fakeBody.style.background = "none";
  15. fakeBody.appendChild(div);
  16. return function(q){
  17. div.innerHTML = "&shy;<style media=\"" + q + "\"> #mq-test-1 { width: 42px; }</style>";
  18. docElem.insertBefore( fakeBody, refNode );
  19. bool = div.offsetWidth === 42;
  20. docElem.removeChild( fakeBody );
  21. return {
  22. matches: bool,
  23. media: q
  24. };
  25. };
  26. }( w.document ));
  27. }( this ));