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

☆樱花仙子☆ 提交于 2019-12-23 18:16:03

问题


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.


回答1:


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/




回答2:


Some networks send connection type as http header.

Your best bet would be to do a speed test when user connects and try to store it on the client as cookie



来源:https://stackoverflow.com/questions/6885402/is-there-a-way-to-detect-3g-and-2g-connections-speed-on-mobile-phones-and-handhe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!