0

I am writing a app that is using server side code mainly. But the errors are thrown no matter what ever with a correct connection. Please help.

function checkConnection() {
        var networkState = navigator.connection.type;

        var states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.CELL]     = 'Cell generic connection';
        states[Connection.NONE]     = 'No network connection';

        if (states[networkState]="Unknown connection")
        {
        alert('Get in a trusted Connection: ' + states[networkState]);
        }
        else if (states[networkState]="No network connection")
        {
        alert('Warning: ' + states[networkState]+': Please Get Connection and Reconnect');
        }
        else
        {
        var ref = window.open('http://70.61.212.124:8083', '_self', 'location=no');
        }
    }
Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
user3102608
  • 365
  • 1
  • 3
  • 6
  • 1
    `=` is assignment, `==` compares two references, equals() checks for equality – DoubleDouble Dec 23 '13 at 18:33
  • @DoubleDouble You mean `===` – SLaks Dec 23 '13 at 18:34
  • 4
    Java is to Javascript as car is to carpet. – SLaks Dec 23 '13 at 18:35
  • my bad, thought my question list was sorted to 'Java' tags. Here is a good answer explaining for javascript. http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – DoubleDouble Dec 23 '13 at 18:38
  • Anyway, when you'll get around those errors, I suggest you changing your state of mind. Creating an object and assigning it strings related to the connection type is superfluous. Why don't you just check the connection type? Something like `if (networkState === Connection.UNKNOWN)` etc etc... – Niccolò Campolungo Dec 23 '13 at 18:42

1 Answers1

1

In your statement

 if (states[networkState]="Unknown connection")
            {
            alert('Get in a trusted Connection: ' + states[networkState]);
            }

you are assigning

states[networkState]="Unknown connection"

so value of

states[networkState] would always be "Unknown connection" and this if condition would always be true, as assignment always returns true.

i hope what you really mean to do is

states[networkState]=="Unknown connection"
gaurav5430
  • 12,934
  • 6
  • 54
  • 111