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

65

u/PortablePawnShop Aug 21 '18

Holy shit, I've been doing absurdly complicated workarounds for some of this

5

u/IIIIRadsIIII Aug 22 '18

What’s a good use case for this?

8

u/flaiks Aug 22 '18

hmm I would say rendering different component sets for mobile/desktop ?

5

u/13steinj Aug 22 '18

Because of a "legacy" internal app I support that deals with lots of html tables that have complex "show/hide this column" rules in CSS, that.

Much easier to just have some JS conditionally fire or fire with a swutch than deal with many confusing media queries in the same CSS document. Depending on the implementation you also save in "time it takes for the dom to load because all the CSS has to be parsed and applied".

1

u/IIIIRadsIIII Aug 22 '18

Yesh. Yeah that makes sense there.

6

u/backlashsid Aug 21 '18

Like what?

39

u/PortablePawnShop Aug 22 '18

Clumsily using SetInterval to track window.innerwidth to notify JS of the corresponding CSS media query and sloppily try to fasten JS and CSS together, and more I probably shouldn't admit lol. Self-taught, program recreationally

41

u/lamb_pudding Aug 22 '18

Ho boy. I imagine you've found the window resize event useful these days?

16

u/undercover_geek Aug 22 '18

I'm imagining PortablePawnShop now learning about the window resize event and the slow realisation of how much more time could have been saved.

6

u/roger_comstock Aug 22 '18

If you go this route, don't you end up with thousands of unnecessary resize events?

Is this advice from Mozilla still relevant? https://developer.mozilla.org/en-US/docs/Web/Events/resize#Examples

Edit: Added link

12

u/fucking_passwords Aug 22 '18

There’s definitely a way to detect when the page has stopped resizing.

Also, debounce.

7

u/DOSMasterrace Aug 22 '18

Debounce, baby.

1

u/backlashsid Aug 22 '18

And what was your requirement ? What were you trying to do?

1

u/Meph1k Aug 22 '18

what??

0

u/[deleted] Aug 22 '18

[deleted]

3

u/saitilkE Aug 22 '18

Ok, this is actually quite elegant for a workaround

3

u/hankDraperCo Aug 22 '18

This is what I've done and it works quite nicely. I knew about matchMedia but it is annoying to manage 2 sets of the same card -- one is scss and the other in js

1

u/deadlyicon Aug 22 '18

Can’t you get everything you get from css media queries, in JS, by listening to window resize events and doing a bit of math?

14

u/mort96 Aug 22 '18

Media queries can do more than just detect the screen size; it can tell if the screen is in landscape or portrait, the screen's pixel density, the color bit depth, whether the device's input method allows the user to hover over stuff (as opposed to a touch screen, where you're either not interacting with the screen at all or clicking on something), the light level from the device's light sensor, how precise the user's pointing device is (if any), whether there's a speech synthesizer, whether the page is being printed, etc.

https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries

6

u/dzkn Aug 22 '18

Yes, but now you don't have to