有没有办法检测手机和手持设备上的 3G 和 2G 连接?
比如如果我想在用户使用 3G 时提供高端网站,如果用户使用 2G 时提供高度优化版本。
有没有办法检测手机和手持设备上的 3G 和 2G 连接?
比如如果我想在用户使用 3G 时提供高端网站,如果用户使用 2G 时提供高度优化版本。
在 Android 2.2+ 中有一个 JS 对象。
您可以根据连接类型编写一个供 CSS 使用的类。但据我所知,它在 iOS 上不适用于移动网站。
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/
某些网络将连接类型作为 http 标头发送。
您最好的选择是在用户连接时进行速度测试并尝试将其作为 cookie 存储在客户端