Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to detect 3G and 2G connections speed on mobile phones and handheld devices?

Is there a way to detect 3G and 2G connections on mobile phones and handheld devices?

Like If I want to deliver High-end Website when user is on 3G and Highly optimized version if user is on 2G.

like image 468
Jitendra Vyas Avatar asked Dec 11 '25 15:12

Jitendra Vyas


1 Answers

In Android 2.2+ there's a JS object for that.

You can write out a class for CSS use based on connection type. But it's not available on iOS for mobile web sites as far as I know.

var connection, connectionSpeed, htmlNode, htmlClass;
connection = navigator.connection || {"type":"0"}; // fallback

switch(connection.type) {
  case connection.CELL_3G: connectionSpeed = "mediumbandwidth"; break;
  case connection.CELL_2G: connectionSpeed = "lowbandwidth"; break;
  default: connectionSpeed = 'highbandwidth';
}

/* set the connection speed on the html element
   i.e. <html class="lowbandwidth">
*/
htmlNode = document.body.parentNode;
htmlClass = htmlNode.getAttribute("class") || "";
htmlNode.setAttribute("class", htmlClass + " " + connectionSpeed);

The code is from slide 24 in this presentation:
http://davidbcalhoun.com/present/mobile-performance/

like image 153
Jörg Butzer Avatar answered Dec 14 '25 18:12

Jörg Butzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!