P粉2041364282023-08-22 10:00:46
Edit: This question tickled me, so I built a JSONP webservice on Google App Engine that returns the client's IP address. usage:
<script type="application/javascript"> function getip(json){ alert(json.ip); // 弹出IP地址 } </script> <script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"> </script>
Yeah, no server proxy required.
Pure JS cannot achieve this. If you have a server script that prints output on the same domain, you can send an XMLHttpRequest to read it.
P粉6775730792023-08-22 00:52:51
In the JavaScript standard library, there is no concept of a host or IP address. So you need to access some external service to find the hostname.
I recommend that you host a cgi-bin that looks up the IP address of the hostname and accesses it via JavaScript.