0

I have the following function set up to notify my browser of the current connection type. For some reason this is returning null. AM I doing something wrong? To note, I do not believe getting the value in ScriptNotify is the issue, I am using this same method for other functions which work ok. It seems navigator.connection is null.

JavaScript

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

C#

within a button click event I use InvokeScript to call the function

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

and then

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

        ResultTextBlock.Text = value;
   }

EDIT javascript update

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

        window.external.notify("COT" + connectionType);
    }
Matthew
  • 3,976
  • 15
  • 66
  • 130

1 Answers1

1

Follow this example:

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

Code behind:

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

        }
Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31
  • `navigator.connection` is not available in IE 10 for Windows Phone. I also posted my other updated solution http://stackoverflow.com/questions/22777362/calculating-speed-using-javascript-returns-nan but I cannot determine what the error may be. Maybe you could take a look? – Matthew Apr 04 '14 at 20:23