Skip to content
This repository

Expose matchMedia polyfill so other scripts can use it #76

Closed
wants to merge 1 commit into from

1 participant

Sasha Sklar
Sasha Sklar

The matchMedia polyfill is very useful and it's only a few extra bytes to add it to window so other scripts can get to it. I'm using it along with require.js to conditionally load behavior based on viewport width.

Sasha Sklar Make matchMedia polyfill available to other scripts
The matchMedia polyfill is very useful and it's only a few extra bytes
to add it to window so other scripts can get to it.
06e263c
Sasha Sklar

I'm going to close this and send a pull request through a topic branch instead.

Sasha Sklar sashasklar closed this November 08, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 06, 2011
Sasha Sklar Make matchMedia polyfill available to other scripts
The matchMedia polyfill is very useful and it's only a few extra bytes
to add it to window so other scripts can get to it.
06e263c
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 20 additions and 15 deletions. Show diff stats Hide diff stats

  1. 35  respond.src.js
35  respond.src.js
@@ -247,8 +247,8 @@
247 247
 	(function( win ){
248 248
 		
249 249
 		//for speed, flag browsers with window.matchMedia support and IE 9 as supported
250  
-		if( win.matchMedia ){ return true; }
251  
-
  250
+		if( win.matchMedia ){  return true; } 
  251
+		
252 252
 		var bool,
253 253
 			doc			= document,
254 254
 			docElem		= doc.documentElement,
@@ -263,18 +263,23 @@
263 263
 		div.style.cssText = "position:absolute;top:-99em";
264 264
 		fakeBody.appendChild( div );
265 265
 		
266  
-		div.innerHTML = '_<style media="'+q+'"> #mq-test-1 { width: 9px; }</style>';
267  
-		if( fakeUsed ){
268  
-			docElem.insertBefore( fakeBody, refNode );
269  
-		}	
270  
-		div.removeChild( div.firstChild );
271  
-		bool = div.offsetWidth == 9;  
272  
-		if( fakeUsed ){
273  
-			docElem.removeChild( fakeBody );
274  
-		}	
275  
-		else{
276  
-			fakeBody.removeChild( div );
277  
-		}
278  
-		return bool;
  266
+		win.matchMedia = function(q) {
  267
+			div.innerHTML = '_<style media="'+q+'"> #mq-test-1 { width: 9px; }</style>';
  268
+			if( fakeUsed ){
  269
+				docElem.insertBefore( fakeBody, refNode );
  270
+			}	
  271
+			div.removeChild( div.firstChild );
  272
+			bool = div.offsetWidth == 9;  
  273
+			if( fakeUsed ){
  274
+				docElem.removeChild( fakeBody );
  275
+			}		
  276
+			else{
  277
+				fakeBody.removeChild( div );
  278
+			}
  279
+			
  280
+    		return { matches: bool, media: q };
  281
+		};
  282
+		return matchMedia(q).matches;
  283
+		
279 284
 	})( this )
280 285
 );
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.