Is it possible using only JavaScript to obtain the user's IP Address? If so, how?
-
1Basically an exact duplicate of http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript – merkuro Jun 25 '09 at 22:58
-
1Which is solved with json here: http://stackoverflow.com/questions/102605/can-i-lookup-the-ip-address-of-a-hostname-from-javascript – Vladiat0r Jun 25 '09 at 23:23
-
Does this answer your question? [How to get client's IP address using JavaScript?](https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript) – Hermanboxcar Jan 30 '22 at 05:48
4 Answers
I don't think so. You'll need to use a server side language. Or find a service maybe you could use with AJAX, but I'm not sure if one exists.

- 479,566
- 201
- 878
- 984
I've actually been toying around with this myself. You can obtain the user's local IP address if they are using certain browsers using JavaScript with WebRTC. WebRTC is currently supported by Chrome, Firefox, and Opera, so it doesn't work across all browsers, but its a start. A great solution is offered by mido in an earlier question titled How to get client’s IP address using javascript only?.
This is the code I am currently attempting to alter, so I can save the IP addresses to variables instead of just displaying them. I haven't figured that part out yet, but it should help you out. Just cut and paste to a text file and open in your browser.
<html>
<body>
<p id=saveIP> Replace this with IP </p>
<script>
function findIP(onNewIP) { // onNewIp - your listener function for new IPs
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
var pc = new myPeerConnection({iceServers: []}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;
//window.saveIP = pc;
//window.saveIP = localIPs; // Returns [object, object] or JSON.stringfy returns {}
function ipIterate(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
}
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(ipIterate);
});
pc.setLocalDescription(sdp, noop, noop);
}, noop); // create offer and set local description
pc.onicecandidate = function(ice) { //listen for candidate events
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
};
}
var ul = document.createElement('ul');
ul.textContent = 'Your IPs are: '
document.body.appendChild(ul);
function addIP(ip) {
console.log('got ip: ', ip);
var li = document.createElement('li');
li.textContent = ip;
window.saveIP = ip; // <--value captured is [object HTMLParagraph]; JSON.stringify returns {}
ul.appendChild(li);
}
findIP(addIP);
document.getElementById('saveIP').innerHTML = JSON.stringify(window.saveIP);
</script>
</body>
</html>
No. It is strictly client-side so it will use some secondary technology to find the IP Address.
A google search provides many options.

- 4,275
- 2
- 20
- 20
-
You mind giving an example of this secondary service. Also, how would that work since that would then mean *my* web server would be forwarding the request ... so the secondary service would simply return the IP address of my web server and not the clients browser – Jun 25 '09 at 23:01
-
I actually said 'secondary technology' such as PHP or .NET which will run on your server. Your question specifically asked if you could use ONLY javascript, which you can't. You will need to have a server-side script provide you with that info. If you are using the Prototype or jQuery then you can perform AJAX requests with ease and return the value you want. For more information I suggest any of the related SO questions above or refer to http://javascript.about.com/library/blip.htm which outlines how to do it with Java, PHP, ASP, .NET, and CF – Paulo Jun 26 '09 at 00:07
What about using one of those online services that tell you the IP address of the requestor. I've only seen them in the context of a html view. But my idea is that you make some request, and the response would contain the ip info (in some crude way).

- 12,551
- 11
- 67
- 97