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

前端 未结 2 1884
一生所求
一生所求 2020-12-19 04:48

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 WI

相关标签:
2条回答
  • 2020-12-19 05:28

    I wrote a small utility to do this. You can try it here http://ashanbh.github.io/detectClientSpeed/example2.html and fork it on github: 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>
    
    0 讨论(0)
  • 2020-12-19 05:42

    Network Information API (This is an experimental technology):

    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.

    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);
    
    0 讨论(0)
提交回复
热议问题