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

17

u/occsceo Aug 21 '18

wait...wait...wait. how new is this? anyone have this running in a production app?

25

u/dudeatwork Aug 22 '18

I'm using it in a production site. Browser support is good - https://caniuse.com/#feat=matchmedia - goes back to IE 10.

1

u/monkh Aug 23 '18

MDN listed android browser, chrome for android and Samsung browser as unknown.

Guess they need to update that.

4

u/nickgcattaneo Aug 22 '18

Yep - we use this to detect breakpoints that are tied to various functionality we only support in certain views.

2

u/Canowyrms Aug 22 '18

I used it a year and a half ago on a production site. The site is no longer live though. Browser support back then sufficed for that project.

2

u/kevinkace Aug 22 '18

Definitely have used them. Check https://www.guildwars2.com/path-of-fire

1

u/totomo26 Aug 22 '18

Did you work on that website? If yes, what's with those weird class names?

1

u/kevinkace Aug 22 '18

Yup, I am the creator of that site ๐Ÿ˜€

We use modular-css a lot here, the class names are prefixed with a hash of the file path so they are namespaced.

1

u/totomo26 Aug 22 '18

Oh nice. Cool website by the way.

1

u/kevinkace Aug 22 '18

Thank you!

2

u/Rhym Aug 22 '18

I use it for enabling carousels in larger screen sizes, and just falling back to showing the first "slide" in smaller screen sizes.

-14

u/esr360 Aug 21 '18

This has been around for ages. I've been using it in production for years. Read the docs sometimes guys lol, this is a standard documented API.

17

u/senocular Aug 21 '18

this is a standard documented API

Its in draft form currently: https://www.w3.org/TR/cssom-view-1/ So "standard" is a little bit of a stretch (in that its current form has not been formally standardized). The MDN page for MediaQueryList tags this as "This is an experimental technology" as well, so its understandable why this wouldn't necessarily be in widespread use.

2

u/XiMingpin91 Aug 21 '18

Explains why it isnโ€™t more widely known

-10

u/esr360 Aug 22 '18

Guess I'm too much of a cowboy. No doubt about it that you're right here. But the fact remains that I've been using it for literally years with no issues. It's hardly new.

0

u/[deleted] Aug 22 '18

[deleted]

-4

u/esr360 Aug 22 '18

The same is true of any API. You can't guarantee that any API will never become deprecated or obsolete. You just have to ask yourself if it's likely. To me it seems very likely that this API will work as-is for the next few years at least, and if I had any genuine concerns I would use a polyfill.

0

u/[deleted] Aug 22 '18

[deleted]

1

u/AndrewGreenh Aug 22 '18

This is not part of ECMAScript, this belongs to browser APIs.

-2

u/esr360 Aug 22 '18

Question whatever you want, but the fact is I've been benefiting from this API for years already and will continue to benefit from it for years to come. I don't know what else to tell you.

0

u/[deleted] Aug 22 '18

[deleted]

2

u/esr360 Aug 22 '18

And I understand that. But when this has been working as expected for years already and has full support on caniuse, I'm suggesting that in this particular case the risk is very much worth it, because it's an extremely useful API.

-4

u/occsceo Aug 21 '18

you aren't wrong (rtfm). :doh: