The Network Information API enables web applications to access the underlying connection information of the device.
### What's new? * Reintroduce the `type` attribute from the [20110607](http://www.w3.org/TR/2011/WD-netinfo-api-20110607) spec, but with a different set of connection types. * Remove the `bandwidth` and `metered` attributes from the [20121129](http://www.w3.org/TR/2012/WD-netinfo-api-20121129/) spec. * Keep the `onchange` event introduced in the [20121129](http://www.w3.org/TR/2012/WD-netinfo-api-20121129/) spec, but renamed to `ontypechange`. For the rationale for the changes, see the [Discussion](https://github.com/w3c-webmob/netinfo#discussion) section of the [Review of Apps that Use Network Information](https://github.com/w3c-webmob/netinfo).
## Use cases and requirements This document attempts to address the [requirements](https://github.com/w3c-webmob/netinfo/blob/master/README.md) from the [Review of Apps that Use Network Information](https://github.com/w3c-webmob/netinfo) document published by the Web and Mobile Interest Group. Those are: * Provide access to the connection type the system is using to communicate with the network (e.g., cellular, Wi-Fi, BlueTooth, other, or none). This information needs to be available either immediately on page load or as close as possible to it. * Provide a means for scripts to be notified if the connection type changes. This is to allow developers to make dynamic changes to the DOM and/or inform the user that the network connection type has changed (and that it could impact them in some way).
## Examples of usage The following are simple examples of how to use the API. More [sophisticated examples](https://github.com/w3c/netinfo/blob/gh-pages/examples/) are available on GitHub. For examples of the kinds of applications one can build with this API, see the [Review of Apps that Use Network Information](https://github.com/w3c-webmob/netinfo).
// Get the connection type.
var type = navigator.connection.type;

function changeHandler(e) {
  // Handle change of connection type here.
}

// Register for event changes.
navigator.connection.ontypechange = changeHandler;

// Alternatively.
navigator.connection.addEventListener('typechange', changeHandler);
## Connection types This section defines the connection types:
bluetooth
The user agent is using a bluetooth connection.
cellular
The user agent is using a cellular connection (e.g., EDGE, 3G, 4G, etc.).
ethernet
The user agent is using an ethernet connection.
none
The user agent will not contact the network when the user follows links or when a script requests a remote page (or knows that such an attempt would fail) - i.e., equivalent to `navigator.onLine === false` in HTML.
wifi
The user agent is using a wifi connection.
other
The user agent is using a connection type that is not one of enumerated connection types.
unknown
The user agent has established a network connection, but is unable to determine what the connection type is.
## Extensions to the `Navigator` interface
readonly attribute NetworkInformation connection
### The `connection` attribute The `connection` attribute, when getting, returns an object that implements the `NetworkInformation` interface.
## The `NetworkInformation` interface The `NetworkInformation` interface provides a means to access information about the network connection the user agent is currently using.
readonly attribute ConnectionType type
attribute EventHandler ontypechange
### `type` attribute The `type` attribute, when getting, returns the connection type that the user agent is using. Whenever the user agent needs to make the `navigator.connection.type` attribute of a `Window` or `WorkerGlobalScope` object return a new connection type, the user agent MUST [queue a task](http://www.whatwg.org/specs/web-apps/current-work/#queue-a-task) to [fire a simple event](http://www.whatwg.org/specs/web-apps/current-work/#fire-a-simple-event) named `typechange` at the `NetworkInformation` object and wait until that task begins to be executed before actually returning a new value.
## The `ConnectionType` enum
cellular
bluetooth
ethernet
wifi
other
none
unknown
There is only one class of product that can claim conformance to this specification: a user agent.
## Acknowledgments This document reuses text from the [[!HTML]] specification, edited by Ian Hickson, as permitted by the license of that specification.