Skip to content

Instantly share code, notes, and snippets.

@shazron
Created June 19, 2012 23:05
Show Gist options
  • Select an option

  • Save shazron/2957061 to your computer and use it in GitHub Desktop.

Select an option

Save shazron/2957061 to your computer and use it in GitHub Desktop.
Test navigator.network.connection.type
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">
<!-- iPad/iPhone specific css below, add after your main css >
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
-->
<!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
<script type="text/javascript" charset="utf-8" src="cordova-1.8.1.js"></script>
<script type="text/javascript">
function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
/* When this function is called, Cordova has been initialized and is ready to roll */
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
for more details -jm */
function onDeviceReady()
{
// do your thing!
setInterval(updateStatus, 1000);
}
var i = 0;
function updateStatus(){
var d = document.getElementById("status");
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';
d.innerHTML = 'Connection type: ' + states[networkState] + " (" + (i++) + ")";
}
</script>
</head>
<body onload="onBodyLoad()">
<div id="status">Nothing</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment