Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 47 lines (36 sloc) 1.7 kb
b848ceb David Knight Added 'David Knight' to the authors comment list
knightdr authored
1 /*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas, David Knight. Dual MIT/BSD license */
50c64c8 Paul Irish big refactor to incorporate nicholas's technique.
authored
2
28929cc David Knight Complete rewrite in an effort to gain performance
knightdr authored
3 window.matchMedia || (window.matchMedia = function() {
4 "use strict";
35b041e just some edits to coding style, and added "use strict" for linty freshn...
Scott Jehl authored
5
47d6e6e David Knight Added comments, fixed typo, changed appendChild to insertBefore, minor c...
knightdr authored
6 // For browsers that support matchMedium api such as IE 9 and webkit
7 var styleMedia = (window.styleMedia || window.media);
795df18 Paul Irish IE is the whiniest fixes #10
authored
8
47d6e6e David Knight Added comments, fixed typo, changed appendChild to insertBefore, minor c...
knightdr authored
9 // For those that don't support matchMedium
28929cc David Knight Complete rewrite in an effort to gain performance
knightdr authored
10 if (!styleMedia) {
11 var style = document.createElement('style'),
47d6e6e David Knight Added comments, fixed typo, changed appendChild to insertBefore, minor c...
knightdr authored
12 script = document.getElementsByTagName('script')[0],
13 info = null;
795df18 Paul Irish IE is the whiniest fixes #10
authored
14
28929cc David Knight Complete rewrite in an effort to gain performance
knightdr authored
15 style.type = 'text/css';
16 style.id = 'matchmediajs-test';
795df18 Paul Irish IE is the whiniest fixes #10
authored
17
47d6e6e David Knight Added comments, fixed typo, changed appendChild to insertBefore, minor c...
knightdr authored
18 script.parentNode.insertBefore(style, script);
19
20 // 'style.currentStyle' is used by IE <= 8 and 'window.getComputedStyle' for all other browsers
953faa1 Lukas von Blarer Fixing error in Firefox 3.6
luksak authored
21 info = ('getComputedStyle' in window) && window.getComputedStyle(style, null) || style.currentStyle;
795df18 Paul Irish IE is the whiniest fixes #10
authored
22
28929cc David Knight Complete rewrite in an effort to gain performance
knightdr authored
23 styleMedia = {
24 matchMedium: function(media) {
47d6e6e David Knight Added comments, fixed typo, changed appendChild to insertBefore, minor c...
knightdr authored
25 var text = '@media ' + media + '{ #matchmediajs-test { width: 1px; } }';
35b041e just some edits to coding style, and added "use strict" for linty freshn...
Scott Jehl authored
26
47d6e6e David Knight Added comments, fixed typo, changed appendChild to insertBefore, minor c...
knightdr authored
27 // 'style.styleSheet' is used by IE <= 8 and 'style.textContent' for all other browsers
28 if (style.styleSheet) {
29 style.styleSheet.cssText = text;
30 } else {
31 style.textContent = text;
32 }
795df18 Paul Irish IE is the whiniest fixes #10
authored
33
47d6e6e David Knight Added comments, fixed typo, changed appendChild to insertBefore, minor c...
knightdr authored
34 // Test if media query is true or false
35 return info.width === '1px';
28929cc David Knight Complete rewrite in an effort to gain performance
knightdr authored
36 }
37 };
38 }
154509a Paul Irish add back examples.
authored
39
28929cc David Knight Complete rewrite in an effort to gain performance
knightdr authored
40 return function(media) {
41 return {
42 matches: styleMedia.matchMedium(media || 'all'),
43 media: media || 'all'
44 };
45 };
b848ceb David Knight Added 'David Knight' to the authors comment list
knightdr authored
46 }());
Something went wrong with that request. Please try again.