Is there any way you can find the IP of the server that the browser is connected to? For e.g. if the browser is accessing http://www.google.com, can we tell in any way what
You cannot get this in general. From Javascript, you can only get the HTTP header, which may or may not have an IP address (typically only has the host name). Part of the browser's program is to abstract the TCP/IP address away and only allow you to deal with a host name.
Can do this thru a plug-in like Java applet or Flash, then have the Javascript call a function in the applet or vice versa (OR have the JS call a function in Flash or other plugin ) and return the IP. This might not be the IP used by the browser for getting the page contents. Also if there are images, css, js -> browser could have made multiple connections. I think most browsers only use the first IP they get from the DNS call (that connected successfully, not sure what happens if one node goes down after few resources are got and still to get others - timers/ ajax that add html that refer to other resources).
If java applet would have to be signed, make a connection to the window.location (got from javascript, in case applet is generic and can be used on any page on any server) else just back to home server and use java.net.Address to get IP.
I think you may use the callback from a JSONP request or maybe just the pure JSON data using an external service but based on the output of javascript location.host
that way:
$.getJSON( "//freegeoip.net/json/" + window.location.host + "?callback=?", function(data) {
console.warn('Fetching JSON data...');
// Log output to console
console.info(JSON.stringify(data, null, 2));
});
I'll use this code for my personal needs, as first I was coming on this site for the same reason.
You may use another external service instead the one I'm using for my needs. A very nice list exist and contains tests done here https://stackoverflow.com/a/35123097/5778582
Not sure how to get the IP address specifically, but the location object provides part of the answer.
e.g. these variables might be helpful:
self.location.host
- Sets or retrieves the hostname and port number of the locationself.location.hostname
- Sets or retrieves the host name part of
the location or URL.Try this as a shortcut, not as a definitive solution (see comments):
<script type="text/javascript">
var ip = location.host;
alert(ip);
</script>
This solution cannot work in some scenarios but it can help for quick testing. Regards
Actually there is no way to do this through JavaScript, unless you use some external source. Even then, it might not be 100% correct.