Web/API/Proximity Events

From Get docs

This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.


The proximity events are a handy way to know when a user is close to a device. These events make it possible to react to such a change, for example by shutting down the screen of a smartphone when the user is having a phone call with the device close to their ear.

Note: Obviously, the API requires the device to have a proximity sensor, which are mostly available only on mobile devices. Devices without such a sensor may support those events but will never fire them.


Proximity Events

When the device proximity sensor detects a change between the device and an object, it notifies the browser of that change. When the browser gets such a notification, it fires a DeviceProximityEvent for any change, and a UserProximityEvent event in the case of a more rough change.

This event can be captured at the window object level by using the addEventListener method (using the deviceproximity or userproximity event name) or by attaching an event handler to the window.ondeviceproximity or window.onuserproximity properties.

Once captured, the event object gives access to different kinds of information:

  • The DeviceProximityEvent event provides an exact match for the distance between the device and the object through its value property. It also provides the closest and farthest distance the device is able to detect something through its min and max properties.
  • The UserProximityEvent event provides a rough approximation of the distance, expressed through a boolean. The UserProximityEvent.near property is true if the object is close or false if the object is far.

Example

window.addEventListener('userproximity', function(event) {
  if (event.near) {
    // let's power off the screen
    navigator.mozPower.screenEnabled = false;
  } else {
    // Otherwise, let's power on the screen
    navigator.mozPower.screenEnabled = true;
  }
});

Specifications

Specification Status Comment
Proximity SensorThe definition of 'Proximity Events' in that specification. Working Draft Initial specification

Browser compatibility

DeviceProximityEvent

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet

DeviceProximityEvent

Experimental'Non-standard'

Chrome

No support No

Edge

No support No

Firefox Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support ? — 61


IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 15 — 61


Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

max

Experimental'Non-standard'

Chrome

No support No

Edge

No support No

Firefox Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support ? — 61


IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 15 — 61


Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

min

Experimental'Non-standard'

Chrome

No support No

Edge

No support No

Firefox Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support ? — 61


IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 15 — 61


Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

value

Experimental'Non-standard'

Chrome

No support No

Edge

No support No

Firefox Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support ? — 61


IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 15 — 61


Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.'
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.'
Non-standard. Expect poor cross-browser support.
See implementation notes.'
See implementation notes.
User must explicitly enable this feature.'
User must explicitly enable this feature.


UserProximityEvent

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet

UserProximityEvent

Experimental'Non-standard'

Chrome

No support No

Edge

No support No

Firefox Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support ? — 61


IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support ? — 61


Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

near

Experimental'Non-standard'

Chrome

No support No

Edge

No support No

Firefox Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support ? — 61


IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android Full support 62

Notes' Disabled'

Full support 62

Notes' Disabled'

Notes' See bug 1462308. Disabled' From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 15 — 61


Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.'
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.'
Non-standard. Expect poor cross-browser support.
See implementation notes.'
See implementation notes.
User must explicitly enable this feature.'
User must explicitly enable this feature.


See also