0

I was trying to create a github.io site which have a URL box where we can enter any URL and as an output, I need the IP address of the URL entered by the user. I tried several methods but none of them worked. I tried to get solutions from so many similar questions in stackoverflow but all of them are returning the users IP address instead of the IP address of the URL the user entered.

Is there a way to create this in Only Javascript (+HTML) ?

  • Unfortunately there is no way of doing it. You need to use an external service to resolve an IP address from a hostname. – Zafar Mar 20 '14 at 15:07
  • What you want is a DNS lookup and there isn't a library in JS that does this. You need to do a ajax call back to a host that can do the lookup for you. – scrappedcola Mar 20 '14 at 15:07
  • Its ok if I need to use an external service but how will I implement it using only Javascript ? @scrappedcola , How can I do a ajax call back to a host that can do a lookup ? Can you write a sample code to understand ? – Anirudh Anand Mar 20 '14 at 15:08
  • There are many tutorials on ajax out there. I would sugest for the simplest code to use jQuery, a js library that wraps up many common activities into functions, http://learn.jquery.com/ajax/. Though a plain js example is here: http://www.codecoffee.com/articles/ajax.html – scrappedcola Mar 20 '14 at 15:14
  • @Endless Thank you so much for your reply. But in order to get the correct IP, I should pass the user entered hostname (From URL) when the function is called to the external service right ? that is the problem. How can I send the URL as argument ? – Anirudh Anand Mar 20 '14 at 15:27

1 Answers1

0

Browsers strip the IP address which they get back from a XmlHttpRequest before they pass it to the Javascript engine. services which provide you with your external IP do this through the TCP/IP wrapper which the server receives, not through the information from the request itself.

However, It could technically be possible, not by making a direct call, but by contacting a DNS server. I have looked into this, and there is a plugin for node.js which you can use for a dns lookup: https://github.com/tjfontaine/node-dns. I know that node.js is a serverbased platform, but it might be possible to adapt this to local JS.

Nzall
  • 3,189
  • 4
  • 25
  • 57
  • You say it would be a security risk, but there's easy enough other ways to get someone's ip like using a xhr to a server that returns your ip. Could you explain how it's a security risk to give js access to your ip? – Joeytje50 Mar 20 '14 at 15:19
  • i'll drop the mention of a security risk since that might be overblown. Browser do strip the IP address, though, both on requests and responses. Servers which send your IP address back don't retrieve this IP adress from the xmlhttprequest directly. – Nzall Mar 20 '14 at 15:26