Skip to content

Instantly share code, notes, and snippets.

@louisremi
Created May 18, 2011 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save louisremi/978531 to your computer and use it in GitHub Desktop.
Save louisremi/978531 to your computer and use it in GitHub Desktop.
matchMedia Demo
// matchMedia is supported and the device has a small screen
if ( "matchMedia" in window
&& window.matchMedia( "(max-device-width: 800px)" )
) {
Array.prototype.forEach.call(
// for each element matching the selector...
document.querySelectorAll(
"a[href*='youtube.com/watch'], a[href*='flickr.com']"
),
// ...replace the href with their mobile equivalent
function( elem ) {
if ( /flickr\.com\/?(.*)$/.test( elem.href ) ) {
elem.href = "http://m.flickr.com/" + RegExp.$1;
} else {
elem.href =
"http://m.youtube.com/watch?v=" +
elem.href.replace( /.*?[?&]v=(.*?)(?:&|$)/, "$1" );
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment