Magnetometer: x property

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 x read-only property of the Magnetometer interface returns a number specifying the magnetic field around the device's x-axis.

Value

A Number.

Examples

The magnetometer is typically read in the reading event callback. In the example below this occurs sixty times a second.

js
let magSensor = new Magnetometer({ frequency: 60 });

magSensor.addEventListener("reading", (e) => {
  console.log(`Magnetic field along the X-axis ${magSensor.x}`);
  console.log(`Magnetic field along the Y-axis ${magSensor.y}`);
  console.log(`Magnetic field along the Z-axis ${magSensor.z}`);
});
magSensor.start();

Specifications

Specification
Magnetometer
# magnetometer-x

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
x
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.