Skip to content

Instantly share code, notes, and snippets.

@brianlmoon
Last active December 13, 2015 19:28
Show Gist options
  • Select an option

  • Save brianlmoon/4962688 to your computer and use it in GitHub Desktop.

Select an option

Save brianlmoon/4962688 to your computer and use it in GitHub Desktop.

Revisions

  1. brianlmoon revised this gist Feb 15, 2013. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -18,13 +18,7 @@ function getWindowMediaWidth() {
    ];

    for(var x = 0; x<values.length; x++){
    console.log(x + ": " + values[x]);
    }

    for(var x = 0; x<values.length; x++){

    if (window.matchMedia("(min-width: " + values[x] + "px) and (max-width: " + values[x] + "px)").matches) {
    console.log("Matched " + x + ": " + values[x]);
    return values[x];
    }
    }
  2. brianlmoon renamed this gist Feb 15, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. brianlmoon created this gist Feb 15, 2013.
    31 changes: 31 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    function getWindowMediaWidth() {

    if(!window.matchMedia){
    // it should be this, so return it when we can't
    // figure it out. Of course, it does not do a lot
    // of good if the browser does not support media
    // queries.
    return document.body.clientWidth;
    }

    // it should be one of these values starting with the
    // first one that makes the most sense
    var values = [
    document.body.clientWidth, // Chrome returns this
    window.innerWidth, // Firefox returns this because
    window.outerWidth, // this is the same as above on browsers with no window borders
    screen.width // last ditch effort here
    ];

    for(var x = 0; x<values.length; x++){
    console.log(x + ": " + values[x]);
    }

    for(var x = 0; x<values.length; x++){

    if (window.matchMedia("(min-width: " + values[x] + "px) and (max-width: " + values[x] + "px)").matches) {
    console.log("Matched " + x + ": " + values[x]);
    return values[x];
    }
    }
    }