Home >Web Front-end >JS Tutorial >How Can I Perform a DNS Lookup in Client-Side Javascript?

How Can I Perform a DNS Lookup in Client-Side Javascript?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-13 11:35:40496browse

How Can I Perform a DNS Lookup in Client-Side Javascript?

DNS Lookup in Client-Side Javascript

Performing a DNS lookup from the client's computer using client-side Javascript presents a challenge, as Javascript lacks direct access to the DNS service.

Edit: JSONP Webservice Solution

An updated solution emerged from the community: a JSONP webservice hosted on Google App Engine. This webservice returns the client's IP address. To use it:

function getip(json){
  alert(json.ip); // alerts the ip address
}
<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"> </script>

This method eliminates the need for server proxies.

Original Answer

Prior to the JSONP solution, pure Javascript could not perform DNS lookups. However, a workaround involving a server script under the same domain was suggested. By sending an XMLHttpRequest to read the output of this server script, the IP address could be obtained.

The above is the detailed content of How Can I Perform a DNS Lookup in Client-Side Javascript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn