MediaQueryList.onchange
The onchange
property of the MediaQueryList
interface is an event handler property representing a function that is invoked when the change
event fires, i.e when the status of media query support changes. The event object is a MediaQueryListEvent
instance, which is recognized as a MediaListQuery
instance in older browsers, for backwards compatibility purposes.
Syntax
MediaQueryList.onchange = function() { ... };
Example
var mql = window.matchMedia('(max-width: 600px)'); mql.onchange = (e) => { if (e.matches) { /* the viewport is 600 pixels wide or less */ console.log('This is a narrow screen — less than 600px wide.') } else { /* the viewport is more than than 600 pixels wide */ console.log('This is a wide screen — more than 600px wide.') } }
Specifications
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
onchange |
45 |
79 |
55 |
No |
Yes |
14 |
45 |
45 |
55 |
Yes |
14 |
5.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/onchange