Guest User

Untitled

a guest
Jun 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. /*
  2. * mediaquery function - test whether a CSS media type or query applies
  3. * author: Scott Jehl
  4. * Copyright (c) 2010 Filament Group, Inc
  5. * MIT license
  6. * Developed as a feature of the EnhanceJS Framework (enhancejs.googlecode.com)
  7. * thx to:
  8. - phpied.com/dynamic-script-and-style-elements-in-ie for inner css text trick
  9. - @paul_irish for fakeBody trick
  10. */
  11.  
  12. var mediaquery = (function(doc,undefined){
  13. var cache = {},
  14. docElem = doc.documentElement,
  15. fakeBody = doc.createElement('body'),
  16. testDiv = doc.createElement('div');
  17.  
  18. testDiv.setAttribute('id','ejs-qtest');
  19. fakeBody.appendChild(testDiv);
  20.  
  21. return function(q){
  22. if (cache[q] === undefined) {
  23. var styleBlock = doc.createElement('style');
  24. styleBlock.type = 'text/css';
  25. var cssrule = '@media '+q+' { #ejs-qtest { position: absolute; width: 10px; } }';
  26. if (styleBlock.styleSheet){
  27. styleBlock.styleSheet.cssText = cssrule;
  28. }
  29. else {
  30. styleBlock.appendChild(doc.createTextNode(cssrule));
  31. }
  32. docElem.insertBefore(fakeBody, docElem.firstChild);
  33. docElem.insertBefore(styleBlock, docElem.firstChild);
  34. cache[q] = (testDiv.offsetWidth == 10);
  35. docElem.removeChild(fakeBody);
  36. docElem.removeChild(styleBlock);
  37. }
  38. return cache[q];
  39. }
  40. })(document);
  41.  
  42.  
  43. /*
  44. * EXAMPLE USAGE
  45. */
  46.  
  47. //test 'screen' media type
  48. if(mediaquery('screen')){
  49. //screen media type supported
  50. }
  51.  
  52. //test a handheld media query
  53. if(mediaquery('screen and (device-max-width: 480px)')){
  54. //smartphone/iphone... maybe run some small-screen related dom scripting?
  55. }
Add Comment
Please, Sign In to add comment