Window.matchMedia()

  • Revision slug: DOM/window.matchMedia
  • Revision title: window.matchMedia
  • Revision id: 18539
  • Created:
  • Creator: Marcoos
  • Is current revision? No
  • Comment 7 words added, 3 words removed

Revision Content

{{ DomRef() }}

Summary

Returns a new {{ domxref("MediaQueryList") }} object representing the parsed results of the specified media query string.

Syntax

mql = window.matchMedia(mediaQueryString)

where mediaQueryString is a string representing the media query for which to return a new {{ domxref("MediaQueryList") }} object.

Example

if (window.matchMedia("(min-width: 400px)").matches) {
  /* the view port is at least 400 pixels wide */
} else {
  /* the view port is less than 400 pixels wide */
}

This code lets you handle things differently when the window is very narrow.

See Using media queries from code for additional examples.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ 9 }} {{ CompatGeckoDesktop("6.0") }} 10 PP3 {{ property_prefix("ms") }} {{ CompatNo() }} {{ 5.1 }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatGeckoMobile("6.0") }} {{ CompatNo() }} {{ CompatNo() }} {{ 5 }}

Specification

See also

{{ languages( { "ja": "ja/DOM/window.matchMedia" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>Returns a new {{ domxref("MediaQueryList") }} object representing the parsed results of the specified <a href="/en/CSS/Media_queries" title="en/CSS/Media queries">media query</a> string.</p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval"><em>mql</em> = window.matchMedia(<em>mediaQueryString</em>)
</pre>
<p>where <code>mediaQueryString</code> is a string representing the media query for which to return a new {{ domxref("MediaQueryList") }} object.</p>
<h3 name="Example">Example</h3>
<pre class="brush: js">if (window.matchMedia("(min-width: 400px)").matches) {
  /* the view port is at least 400 pixels wide */
} else {
  /* the view port is less than 400 pixels wide */
}
</pre>
<p>This code lets you handle things differently when the window is very narrow.</p>
<p>See <a href="/en/CSS/Using_media_queries_from_code" title="en/CSS/Using media queries from code">Using media queries from code</a> for additional examples.</p>
<h3>Browser compatibility</h3>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>{{ 9 }}</td> <td>{{ CompatGeckoDesktop("6.0") }}</td> <td>10 PP3 {{ property_prefix("ms") }}</td> <td>{{ CompatNo() }}</td> <td>{{ 5.1 }}</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatGeckoMobile("6.0") }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ 5 }}</td> </tr> </tbody> </table>
</div>
<h3 name="Specification">Specification</h3>
<ul> <li><a class="external" href="http://dev.w3.org/csswg/cssom-view/#the-mediaquerylist-interface" title="http://dev.w3.org/csswg/cssom-view/#the-mediaquerylist-interface">The CSSOM View Module: The MediaQueryList Interface</a></li>
</ul>
<h3>See also</h3>
<ul> <li><a href="/en/CSS/Media_queries" title="En/CSS/Media queries">Media queries</a></li> <li><a href="/en/CSS/Using_media_queries_from_code" title="en/CSS/Using media queries from code">Using media queries from code</a></li> <li>{{ domxref("MediaQueryList") }}</li> <li>{{ domxref("MediaQueryListListener") }}</li>
</ul>
<p>{{ languages( { "ja": "ja/DOM/window.matchMedia" } ) }}</p>
Revert to this revision