网络信息 API

这是 实验性技术
检查 浏览器兼容性表格 在生产中使用这之前。

The Network Information API provides information about the system's connection in terms of general connection type (e.g., 'wifi', 'cellular', etc.). This can be used to select high definition content or low definition content based on the user's connection. The entire API consists of the addition of the NetworkInformation interface and a single property to the Navigator interface: Navigator.connection .

注意: 此特征可用于 Web 工作者 .

范例

Detect connection changes

This example watches for changes to the user's connection.

var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
var type = connection.effectiveType;
function updateConnectionStatus() {
  console.log("Connection type changed from " + type + " to " + connection.effectiveType);
  type = connection.effectiveType;
}
connection.addEventListener('change', updateConnectionStatus);
					

Preload large resources

The connection object is useful for deciding whether to preload resources that take large amounts of bandwidth or memory. This example would be called soon after page load to check for a connection type where preloading a video may not be desirable. If a cellular connection is found, then the preloadVideo flag is set to false . For simplicity and clarity, this example only tests for one connection type. A real-world use case would likely use a switch statement or some other method to check all of the possible values of NetworkInformation.type . Regardless of the type value you can get an estimate of connection speed through the NetworkInformation.effectiveType 特性。

let preloadVideo = true;
var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
if (connection) {
  if (connection.effectiveType === 'slow-2g') {
    preloadVideo = false;
  }
}
					

接口

NetworkInformation
Provides information about the connection a device is using to communicate with the network and provides a means for scripts to be notified if the connection type changes. The NetworkInformation interfaces cannot be instantiated. It is instead accessed through the Navigator 接口。

规范

规范 状态 Comment
网络信息 API
The definition of 'Network Information API' in that specification.
草案 Initial specification

浏览器兼容性

NetworkInformation

更新 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
NetworkInformation
Chrome 61 Edge 79 Firefox No IE No Opera 48 Safari No WebView Android 50 Chrome Android 38 Firefox Android 31 Opera Android 45 Safari iOS No Samsung Internet Android 3.0
downlink
Chrome 61 Edge 79 Firefox No IE No Opera 48 Safari No WebView Android 50 Chrome Android 38 Firefox Android ? Opera Android 45 Safari iOS No Samsung Internet Android 3.0
downlinkMax
Chrome 61
61
Only supported in Chrome OS
Edge No Firefox No IE No Opera No Safari No WebView Android 50 Chrome Android 38 Firefox Android No Opera Android 45 Safari iOS No Samsung Internet Android 3.0
effectiveType
Chrome 61 Edge 79 Firefox No IE No Opera 48 Safari No WebView Android 50 Chrome Android 38 Firefox Android Yes Opera Android 45 Safari iOS No Samsung Internet Android 3.0
onchange
Chrome 61 Edge 79 Firefox No IE No Opera 48 Safari No WebView Android 50 Chrome Android 38 Firefox Android No
不支持 No
On Firefox, the event handler property corresponding to the change event is ontypechange .
Opera Android 45 Safari iOS No Samsung Internet Android 3.0
ontypechange
Chrome No Edge No Firefox No IE No Opera ? Safari No WebView Android No Chrome Android No Firefox Android ? Opera Android ? Safari iOS No Samsung Internet Android No
rtt
Chrome 61 Edge 79 Firefox No IE No Opera 48 Safari No WebView Android 50 Chrome Android 38 Firefox Android No Opera Android 45 Safari iOS No Samsung Internet Android 3.0
saveData
Chrome 65 Edge 79 Firefox No IE No Opera Yes Safari No WebView Android 65 Chrome Android 65 Firefox Android ? Opera Android Yes Safari iOS No Samsung Internet Android 9.0
type
Chrome 61
61
Only supported in Chrome OS
Edge No Firefox No IE No Opera No Safari No WebView Android 50 Chrome Android 38 Firefox Android No Opera Android 45 Safari iOS No Samsung Internet Android 3.0
Available in workers
Chrome 61 Edge 79 Firefox No IE No Opera 48 Safari No WebView Android 50 Chrome Android 38 Firefox Android 53 Opera Android 45 Safari iOS No Samsung Internet Android 3.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。
更新 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
connection
Chrome 61 Edge ≤79 Firefox Yes
Disabled
Yes
Disabled
Disabled This feature is behind the dom.netinfo.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE No Opera Yes Safari No WebView Android 50 Chrome Android 38 Firefox Android 14
14
The Network API is enabled by default. Can be disabled using the dom.netinfo.enabled preference.
Opera Android 37 Safari iOS No Samsung Internet Android 3.0

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅