5

I'm looking into a way for JavaScript to understand more than one media queries. At this point I've the following code to check the width of the browser, but I want to create more queries. Google (and Stack) only showed me the code I've already written. The only way what I see is to use enquire.js, but I want to know if there isn't another way.

$(function() {
// Screen width check 
if (matchMedia) {
    var mq = window.matchMedia("(min-width: 992px)");
    mq.addListener(WidthChange);
    WidthChange(mq);
}

// Run function mq (check screen width)
function WidthChange(mq) {
    // Screen size +992px
    if (mq.matches) {
        .....
    }
    // Screen size -992px
    else {
        .....           
    }
// End widthChange(mq) function
}
// End Function script
});

So this code will only check for min-width: 992px, but now I want atleast 3 more.

2
  • Just to be clear, is your goal to find out the screen width of the user's browser? Commented Jan 18, 2016 at 13:03
  • 1
    @EthanFischer No, I want css media queries like media screen (min-width 999px) { } etc in Javascript/Jquery. Commented Jan 18, 2016 at 13:10

1 Answer 1

2

You can match multiple things by either having two (or more) query's or making a joint one.

Multiple query's

function screenIsWide() {
  var mq = window.matchMedia("(min-width: 992px)");
  return mq.matches;
}

function screenIsToWide() {
  var mq = window.matchMedia("(max-width: 9001px)");
  return mq.matches;
}

function isPrint() {
   var mq = window.matchMedia("print");
   return mq.matches;
}

console.log(screenIsWide() + " " + isPrint() + " " + screenIsToWide());

if(screenIsWide() && isPrint() && screenIsToWide()) {
   // do stuff
}

Joint

You can make this really easy just as in normal CSS Read more here w3schools.com

function isScreenAndIsWide() {
  var mq = window.matchMedia("screen and (min-width: 992px)");
  return mq.matches;
}

console.log(isScreenAndIsWide());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.