javascript 如何使用javascript检测移动连接是2G/3G/WIFI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25968020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:18:47  来源:igfitidea点击:

how to detect the mobile connection is 2G/3G/WIFI using javascript

javascript

提问by user1708054

i have a similar requirement as in link below but i have to handle it by using JavaScript. where i have to detect whether the mobile internet connection is 2g/3g or it is WIFI . based on connection i have to perform diffent operations.. note : mobile can b of any OS like andriod/ iOS/BB .. i need to handle any mobile OS.

我有一个与下面链接类似的要求,但我必须使用 JavaScript 来处理它。我必须检测移动互联网连接是 2g/3g 还是 WIFI。基于连接我必须执行不同的操作.. 注意:移动可以 b 任何操作系统,如 andriod/iOS/BB ..我需要处理任何移动操作系统。

Is there a way to detect what kind of connection I'm using ? WiFi, 3G or Ethernet?

有没有办法检测我使用的连接类型?WiFi、3G 还是以太网?

request masters to help me with inputs. thanks :)

请求大师帮助我输入。谢谢 :)

回答by CD..

Network Information API(This is an experimental technology):

网络信息API(这是一项实验性技术):

The Network Information API provides information about the system's connection, which is in term of general connection type (e.g., 'wifi', 'cellular', etc.). This can be used to select high definition content or low definition content based on the user's connection. The entire API consists of the addition of the domxref("NetworkInformation") interface and a single property to the Navigator interface: Navigator.connection.

网络信息 API 提供有关系统连接的信息,这是根据一般连接类型(例如,“wifi”、“蜂窝”等)。这可用于根据用户的连接选择高清内容或低清晰度内容。整个 API 包括添加的 domxref("NetworkInformation") 接口和 Navigator 接口的单个​​属性:Navigator.connection。

var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
var type = connection.type;

function updateConnectionStatus() {
  alert("Connection type is change from " + type + " to " + connection.type);
}

connection.addEventListener('typechange', updateConnectionStatus);

回答by Amit Shanbhag

I wrote a small utility to do this. You can try it here http://ashanbh.github.io/detectClientSpeed/example2.htmland fork it on github: https://github.com/ashanbh/detectClientSpeed

我写了一个小实用程序来做到这一点。你可以在这里尝试 http://ashanbh.github.io/detectClientSpeed/example2.html并在 github 上 fork :https://github.com/ashanbh/detectClientSpeed

Usage:

用法:

<script src="scripts/require.js"></script>
<script>
        requirejs(['scripts/detectSpeed'], function (detectSpeed) {

            //Callback to receive timing information
            var callback = function (timings) {
                console.log(timings);
            }
            detectSpeed.startSpeedCheck("https://s3-us-west-1.amazonaws.com/amit.shanbhag/3g/coffee-apple-iphone-laptop.jpg", callback);

        });
</script>