r/javascript • • Aug 21 '18

Effectively supported in all browsers, JavaScript has its own form of media queries just like CSS.

https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
465 Upvotes

114 comments sorted by

View all comments

-34

u/StoneCypher Aug 21 '18

um, this actually is css, not javascript

0

u/[deleted] Aug 21 '18

[deleted]

-33

u/StoneCypher Aug 21 '18

Seems to work as expected

<html><head><style type="text/css">
  @media (min-width: 400px) { body { background-color: green; } }
  @media (max-width: 399px) { body { background-color: red; } }
</style></head><body>
  <p>hiya</p>
</body></html>

14

u/rabidhamster Aug 22 '18

Which is not even close to what was asked of you. The only thing that matches is the "min-width: 400px". The rest of the syntax is entirely different.

Try this in your CSS:

if (window.matchMedia("(min-width: 400px)").matches) {
  console.log('Larger than 399px');
} else {
  console.log('Less than 400px');
}

See how media queries based in JavaScript might have some utility and use-cases beyond what CSS can provide?

-16

u/StoneCypher Aug 22 '18

I think that you are confusing "I can use css from Javascript" with "this must not be css"

11

u/rabidhamster Aug 22 '18

Except that this isn't necessarily about styling the page. You can run different conditional branching, loops, JSON queries to the server, etc. based on the results of the window width. Just because you personally would use it to make style changes doesn't make it inherently CSS. By that logic, Javascript is just FORTRAN, because they both have "if" statements.

-12

u/StoneCypher Aug 22 '18

OK, try putting some Fortran in there and see what happens

→ More replies (2)

8

u/crobison Aug 22 '18

Don’t be obtuse.

-8

u/StoneCypher Aug 22 '18

Insults won't change my opinion. Neither will mass downvoting.

If the code is defined by the css spec, it's css.

9

u/crobison Aug 22 '18

You are an idiot.

18

u/bearcherian Aug 21 '18

You should probably actually read the linked article and look at its examples. If you still think you're right, I sincerely hope you are not a professional web developer.

-16

u/StoneCypher Aug 22 '18

That's nice

0

u/[deleted] Aug 22 '18

[deleted]

-2

u/StoneCypher Aug 22 '18

No. You said to try it in css, so I did, and it works, because it is css.

Afterwards, the reddit rage squad came to tell me that it hopes I'm not a programmer, because this js interface to css is useful in js.

0

u/[deleted] Aug 22 '18

[deleted]

0

u/StoneCypher Aug 22 '18

CSS in a style block is the same as CSS in an external file, other than timing issues

For clarity, I do understand that you are talking about the call interface, and that I am talking about what it calls

0

u/[deleted] Aug 22 '18

[deleted]

0

u/StoneCypher Aug 22 '18

For clarity, I do understand that you are talking about the call interface, and that I am talking about what it calls

I’m not convinced we’re talking about the same thing here.

good thing it's been such a friendly discussion this entire time, huh

.

What may be tripping you up

i'm really not interested in watching you repeat yourself in progressively more condescending tones.

.

I’m not sure what your angle is

i know.

.

just picking this incorrect argument as your reddit hill to die on.

i think you've wildly overestimated the importance of you yelling at people on reddit.

i think i'll stop responding. i seem to be just encouraging you to escalate

-8

u/twinkiac Aug 22 '18

9

u/tsteuwer Aug 22 '18

Bad bot

-2

u/B0tRank Aug 22 '18

Thank you, tsteuwer, for voting on twinkiac.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

8

u/ejfrodo Aug 22 '18

um, this is actually javascript, not css. unleses this example from the article somehow works in a .css file

if (window.matchMedia("(min-width: 400px)").matches) {
  console.log('Larger than 399px');
} else {
  console.log('Less than 400px');
}

2

u/[deleted] Aug 22 '18 edited Aug 23 '18

i agree with you

although i thought the window object was part of the dom

1

u/DilatedTeachers Aug 22 '18

Did you read the article? And try it in a js script? It's really fun to use!