这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
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
接口:
Navigator.connection
.
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);
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
NetworkInformation
interfaces cannot be instantiated. It is instead accessed through the
Navigator
接口。
| 规范 | 状态 | 注释 |
|---|---|---|
|
网络信息 API
The definition of 'Network Information API' in that specification. |
草案 | Initial specification |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
|
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
|
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
|
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 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
见实现注意事项。
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
connection
|
Chrome 61 | Edge ≤79 | Firefox Yes | IE No | Opera Yes | Safari No | WebView Android 50 | Chrome Android 38 |
Firefox Android
14
|
Opera Android 37 | Safari iOS No | Samsung Internet Android 3.0 |
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。