Magnetometer: Magnetometer() constructor

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The Magnetometer() constructor creates a new Magnetometer object which returns information about the magnetic field as detected by a device's primary magnetometer sensor.

Syntax

js
new Magnetometer()
new Magnetometer(options)

Parameters

options Optional

Options are as follows:

frequency Optional

The desired number of times per second a sample should be taken, meaning the number of times per second that the reading event will be called. A whole number or decimal may be used, the latter for frequencies less than a second. The actual reading frequency depends on device hardware and consequently may be less than requested.

referenceFrame Optional

Either 'device' or 'screen'. The default is 'device'.

Exceptions

SecurityError DOMException

Use of this feature was blocked by a Permissions Policy.

Specifications

Specification
Magnetometer
# dom-magnetometer-magnetometer

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Magnetometer() constructor
Experimental
Chrome – No support
Chrome 56 (Release date: 2017-01-25)
disabled
disabled From version 56 users must explicitly set the #enable-generic-sensor-extra-classes preference to Enabled. To change preferences in Chrome, visit chrome://flags.
Edge – No support
Edge 79 (Release date: 2020-01-15)
disabled
disabled From version 79 users must explicitly set the #enable-generic-sensor-extra-classes preference to Enabled. To change preferences in Edge, visit about:flags.
Firefox – No support
Firefox
footnote No support
Opera – No support
Opera 43 (Release date: 2017-02-07)
disabled
disabled From version 43 users must explicitly set the #enable-generic-sensor-extra-classes preference to Enabled. To change preferences in Opera, visit opera://flags.
Safari – No support
Safari
footnote No support
Chrome Android – No support
Chrome Android 56 (Release date: 2017-02-01)
disabled
disabled From version 56 users must explicitly set the #enable-generic-sensor-extra-classes preference to Enabled. To change preferences in Chrome Android, visit chrome://flags.
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

No support
No support
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.

See also