Safari Developer Library

Developer

StyleMedia Class Reference

Options
Deployment Target:

On This Page

StyleMedia

Inheritance


  • StyleMedia

Conforms To


Not Applicable

Import Statement


Not Applicable Not Applicable

Availability


Available in Safari 5.0 and later.
Available in iOS 4.2 and later.

The StyleMedia class provides a way to evaluate CSS media queries from JavaScript. You do not need to, nor should you, create instances of this class. You access the shared StyleMedia object using the window’s styleMedia property.

  • type Property

    A string that represents the media type of the current view used for rendering the document.

    Declaration

    JavaScript

    readonly attribute DOMString type

    Discussion

    For pages being displayed on screen, this property value is screen. When printing, this property value is print.

    Availability

    Available in Safari 5.0 and later.

    Available in iOS 4.2 and later.

  • Evaluates the given string as a media query and returns the result.

    Declaration

    JavaScript

    boolean matchMedium (in DOMString mediaquery);

    Parameters

    mediaquery

    The media query to evaluate.

    Return Value

    true if the media query is logically true; otherwise, false.

    Discussion

    For example, window.styleMedia.matchMedium("(color)") returns true if the display device is a color device. You can also use this method to check whether the browser supports 3D transforms as follows:

    • if ('styleMedia' in window && window.styleMedia.matchMedium("(-webkit-transform-3d)")) {
    • // Insert 3D code here
    • }

    Or check to see whether the browser supports animations as follows:

    • if ('styleMedia' in window && window.styleMedia.matchMedium("(-webkit-animation)")) {
    • // Insert animation code here
    • }

    Availability

    Available in Safari 5.0 and later.

    Available in iOS 4.2 and later.