How to get the server's IP Address and redirect the user to local web system? JS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #1

    How to get the server's IP Address and redirect the user to local web system? JS

    So far this is my code on how to redirect the user to the local server.
    Code:
    function connectToServer() {
       window.location="http://192.168.1.154/gcc";
    }
    window.onload = connectToServer;
    but what if the ip address of the server changed?
    My idea now is that i need to get the list of all IPs in the network and check every IP if the "/gcc" exist ?
    so it looks like this "http://scanned_IP/gcc"? if exist redirect to that "http://scanned_IP/gcc", else continue scanning. .

    How to do that in javascript?
    I'm using only html with javascript.

    Any replies are appreciated. :D
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    make an AJAX HEAD Request. if you get a 200 OK, the IP exists.

    Comment

    • Exequiel
      Contributor
      • Jul 2012
      • 288

      #3
      thank you for your response dormilich.
      heres my javascript now using XMLHttpRequest or AJAX HEAD Request,
      Code:
      function connectToServer()
      {
          var http = new XMLHttpRequest();
      	var url = "http://192.168.1.154/gcc";//the right servers IP.
          http.open('HEAD', url);
          http.onreadystatechange = function() 
      	{
              if (this.readyState == this.DONE) 
      		{
      			//alert(this.responseText + " - "+ this.status);
      			if(this.status == 0 || this.status == 200)
      			{
      				alert("Url Exist.");
      				document.location.href = url;
      			}
      			else
      			{
      				alert("Url Not Exist.");	
      			}
              }
          };
          http.send();
      }
      
      window.onload = connectToServer;
      even if i put the valid and invalid ip address for the server the result for this.status is always 0, so it will redirect to that ip with /gcc like this "http://192.168.1.155/gcc" - invalid, "http://192.168.1.154/gcc" - valid, how can i get the right output or the call back after visiting the url if the /gcc exist on that IP?

      I tried this in IE and the result is ok no problem in this browser, but on other browsers like chrome, mozilla, opera, and safari its not working to this browser. how can i fixed this?

      thank you. :)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        even if i put the valid and invalid ip address for the server the result for this.status is always 0
        ah, SOP (same origin policy) strikes. you could try to enable CORS (cross origin resource sharing) otherwise you need to route the request through your server (i.e. ask via AJAX your server to make this HEAD request).

        Comment

        • Exequiel
          Contributor
          • Jul 2012
          • 288

          #5
          but how can i enable CORS? i read about CORS but i don't know how to code it in javascript to enable it. . ?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            IIRC you need to set a CORS header (would have to google what it is exactly).

            note: some libraries make it easier to configure AJAX.

            Comment

            Working...