Home  >  Q&A  >  body text

Is it possible to perform a DNS lookup (convert hostname to IP address) using Javascript on the client side?

<p>I want to use client-side Javascript to perform a DNS lookup (see a hostname to IP address mapping from the client machine). Is this possible? </p>
P粉459578805P粉459578805425 days ago471

reply all(2)I'll reply

  • P粉204136428

    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.

    reply
    0
  • P粉677573079

    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.

    reply
    0
  • Cancelreply