The Connection object provides access to the cellular and wi-fi Connection information of the device.
Access this object through the navigator. network interface.
Attribute:
Connection. type
Constant:
Connection. UNKNOWN
Connection. ETHERNET
Connection. WIFI
Connection. CELL_2G
Connection. CELL_3G
Connection. CELL_4G
Connection. NONE
Connection. type
Check the active network connection in use.
Note:
This attribute is a quick method for determining the network connection status and connection type of a device.
Supported platforms:
IOS
Android
BlackBerry WebWorks (OS 5.0 or later)
Simple Example:
<Span style = "font-size: 13px;"> function checkConnection (){
Var networkState = navigator. network. connection. type;
Var states = {};
States [Connection. UNKNOWN] = 'unknown connection ';
States [Connection. ETHERNET] = 'ethernet connection ';
States [Connection. WIFI] = 'wifi connection ';
States [Connection. CELL_2G] = 'cell 2G connection ';
States [Connection. CELL_3G] = 'cell 3G connection ';
States [Connection. CELL_4G] = 'cell 4G connection ';
States [Connection. NONE] = 'no network connection ';
Alert ('Connection type: '+ states [networkState]);
}
CheckConnection (); </span>
Copy the complete sample code:
<! DOCTYPE html>
<Html>
<Head>
<Title> navigator. network. connection. type Example </title>
<Script type = "text/javascript" charset = "UTF-8" src = "phonegap. js"> </script>
<Script type = "text/javascript" charset = "UTF-8">
// Wait for loading PhoneGap
Document. addEventListener ("deviceready", onDeviceReady, false );
// After the PhoneGap is loaded, you can call the PhoneGap method safely.
Function onDeviceReady (){
CheckConnection ();
}
Function checkConnection (){
Var networkState = navigator. network. connection. type;
Var states = {};
States [Connection. UNKNOWN] = 'unknown connection ';
States [Connection. ETHERNET] = 'ethernet connection ';
States [Connection. WIFI] = 'wifi connection ';
States [Connection. CELL_2G] = 'cell 2G connection ';
States [Connection. CELL_3G] = 'cell 3G connection ';
States [Connection. CELL_4G] = 'cell 4G connection ';
States [Connection. NONE] = 'no network connection ';
Alert ('Connection type: '+ states [networkState]);
}
</Script>
</Head>
<Body>
<P> A dialog box will report the network state. </p>
</Body>
</Html>
Author: LuoXianXion