Can You Get A Users Local LAN IP Address Via JavaScript?
As it turns out, the recent WebRTC extension of HTML5 allows javascript to query the local client IP address. A proof of concept is available here: http://net.ipcalf.com
This feature is apparently by design, and is not a bug. However, given its controversial nature, I would be cautious about relying on this behaviour. Nevertheless, I think it perfectly and appropriately addresses your intended purpose (revealing to the user what their browser is leaking).
How to get local IP address in javascript html5
I dont think that there is a notion of hosts or ip-addresses in the javascript standard library. So you'll have to access some external service to look up hostnames for you.
Unless you might want to send a request to the server which returns you the host IP address!!
EDIT
In JSP you you can use getRemoteHost() method from HttpServletRequest
to get the IP address of the user.
So you can write something like this -
var ip = '<%=request.getRemoteHost();%>';
^^ the above line is JSP code, this should be part of the JSP file that you return from java servlet container like a tomcat. This does not work in static HTML pages.
How to get local/internal IP with JavaScript
UPDATE
Unfortunately the below answer no longer works as browsers have changed this behavior due to security concerns. See this StackOverflow Q&A for more details.
where I took code from --> Source
You can find a demo at --> Demo
I have modified the source code, reduced the lines, not making any stun requests since you only want Local IP not the Public IP, the below code works in latest Firefox and Chrome:
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(pc.setLocalDescription.bind(pc), noop); // create offer and set local description
pc.onicecandidate = function(ice){ //listen for candidate events
if(!ice || !ice.candidate || !ice.candidate.candidate) return;
var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
console.log('my IP: ', myIP);
pc.onicecandidate = noop;
};
what is happening here is, we are creating a dummy peer connection, and for the remote peer to contact us, we generally exchange ice candidates with each other. And reading the ice candiates we can tell the ip of the user.
Related Topics
Defining a JavaScript Prototype
Jquery's Jquery-1.10.2.Min.Map Is Triggering a 404 (Not Found)
Working with Select Using Angularjs's Ng-Options
How to Access the First Property of a JavaScript Object
How to Format Time Since Xxx E.G. "4 Minutes Ago" Similar to Stack Exchange Sites
Prevent Double Submission of Forms in Jquery
How to Cancel an Http Fetch() Request
What Is the Meaning of "$" Sign in JavaScript
Empty Arrays Seem to Equal True and False at the Same Time
Jquery Click/Toggle Between Two Functions
How to Synchronize a Sequence of Promises
The Value of "This" Within the Handler Using Addeventlistener
How to Use Meteor Methods Inside of a Template Helper
How to Turn This Callback into a Promise Using Async/Await
Is ".Then(Function(A){ Return A; })" a No-Op for Promises