如何在JavaScript中确定连接类型

时间:2014-04-01 04:40:44

标签: c# javascript html5

我设置了以下功能,以通知我的浏览器当前的连接类型。由于某种原因,这将返回null。难道我做错了什么?要注意,我不相信在ScriptNotify中获取值是问题,我使用相同的方法用于其他正常工作的函数。似乎navigator.connection为空。

的JavaScript

function getConnectionType() {
        var connectionType = navigator.connection;
        window.external.notify("Connection type: " + connectionType.toString());
    }

C#

在按钮单击事件中我使用InvokeScript来调用函数

object connectionType = Browser.InvokeScript("getConnectionType");

然后

private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        string value = null;
        value = e.Value.ToString();

        ResultTextBlock.Text = value;
   }

编辑javascript更新

function getConnectionType() {
        //var connectionType = navigator.connection;
        var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
        var connectionType = connection.type

        window.external.notify("COT" + connectionType);
    }

1 个答案:

答案 0 :(得分:1)

请遵循以下示例:

<script type="text/javascript">
        var online = navigator.onLine;
        var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;

        function updateConnectionStatus() {
            alert("Connection bandwidth: " + connection.bandwidth + " MB/s");
            if (connection.metered) {
                alert("The connection is metered!");
            }
        }

        connection.addEventListener("change", updateConnectionStatus);
        updateConnectionStatus();
    </script>

代码背后:

  protected void btnInvoke_Click(object sender, EventArgs e)
        {
           ClientScript.RegisterStartupScript(this.GetType(),
                "getConnection", "updateConnectionStatus()", true);

        }