By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

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

Exequiel
100+
P: 287
So far this is my code on how to redirect the user to the local server.
Expand|Select|Wrap|Line Numbers
  1. function connectToServer() {
  2.    window.location="http://192.168.1.154/gcc";
  3. }
  4. window.onload = connectToServer;
  5.  
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
Oct 18 '14 #1

✓ answered by Dormilich

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.

Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
make an AJAX HEAD Request. if you get a 200 OK, the IP exists.
Oct 18 '14 #2

Exequiel
100+
P: 287
thank you for your response dormilich.
heres my javascript now using XMLHttpRequest or AJAX HEAD Request,
Expand|Select|Wrap|Line Numbers
  1. function connectToServer()
  2. {
  3.     var http = new XMLHttpRequest();
  4.     var url = "http://192.168.1.154/gcc";//the right servers IP.
  5.     http.open('HEAD', url);
  6.     http.onreadystatechange = function() 
  7.     {
  8.         if (this.readyState == this.DONE) 
  9.         {
  10.             //alert(this.responseText + " - "+ this.status);
  11.             if(this.status == 0 || this.status == 200)
  12.             {
  13.                 alert("Url Exist.");
  14.                 document.location.href = url;
  15.             }
  16.             else
  17.             {
  18.                 alert("Url Not Exist.");    
  19.             }
  20.         }
  21.     };
  22.     http.send();
  23. }
  24.  
  25. window.onload = connectToServer;
  26.  
  27.  
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. :)
Oct 19 '14 #3

Dormilich
Expert Mod 5K+
P: 8,639
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).
Oct 19 '14 #4

Exequiel
100+
P: 287
but how can i enable CORS? i read about CORS but i don't know how to code it in javascript to enable it. . ?
Oct 20 '14 #5

Dormilich
Expert Mod 5K+
P: 8,639
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.
Oct 20 '14 #6

Post your reply

Sign in to post your reply or Sign up for a free account.