Window.matchMedia()

  • Revision slug: Web/API/window.matchMedia
  • Revision title: window.matchMedia
  • Revision id: 406963
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Updated compatibility table (IE10 supports matchMedia() unprefixed)Moved From DOM/window.matchMedia to Web/API/window.matchMedia

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 12.1 5.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 3.0 {{CompatGeckoMobile("6.0")}} {{CompatNo}} 12.1 5

Specification

See also

Revision Source

<div>
  {{DomRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a new {{domxref("MediaQueryList")}} object representing the parsed results of the specified <a href="/en-US/docs/CSS/Media_queries" title="CSS/Media queries">media query</a> string.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<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>
<h2 id="Example" name="Example">Example</h2>
<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-US/docs/DOM/Using_media_queries_from_code" title="CSS/Using media queries from code">Using media queries from code</a> for additional examples.</p>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<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</td>
        <td>12.1</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>3.0</td>
        <td>{{CompatGeckoMobile("6.0")}}</td>
        <td>{{CompatNo}}</td>
        <td>12.1</td>
        <td>5</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a class="external" href="http://dev.w3.org/csswg/cssom-view/#the-mediaquerylist-interface">The CSSOM View Module: The MediaQueryList Interface</a></li>
</ul>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/CSS/Media_queries" title="CSS/Media queries">Media queries</a></li>
  <li><a href="/en-US/docs/DOM/Using_media_queries_from_code" title="CSS/Using media queries from code">Using media queries from code</a></li>
  <li>{{domxref("MediaQueryList")}}</li>
  <li>{{domxref("MediaQueryListListener")}}</li>
</ul>
Revert to this revision