有没有一种方法可以检测手机和手持设备上的3G和2G连接速度? [英] Is there a way to detect 3G and 2G connections speed on mobile phones and handheld devices?

本文介绍了有没有一种方法可以检测手机和手持设备上的3G和2G连接速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测手机和手持设备上的3G和2G连接?

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

就像我想在用户使用3G时提供高端网站,而在用户使用2G时提供高度优化的版本一样.

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

推荐答案

在Android 2.2及更高版本中,有一个JS对象.

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

您可以根据连接类型写出一个供CSS使用的类. 但据我所知,iOS在移动网站上不可用.

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);

此代码来自此演示文稿中的幻灯片24:
http://davidbcalhoun.com/present/mobile-performance/

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

这篇关于有没有一种方法可以检测手机和手持设备上的3G和2G连接速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆