Last active
May 12, 2018 06:55
-
-
Save liketaurus/644dbf40cda8a966d27d8a6b061d4d3c to your computer and use it in GitHub Desktop.
How to get local IP address via JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// found at https://stackoverflow.com/a/32841043 | |
// many thanks to author! | |
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; | |
// Firefox & Chrome only! This will not work in Edge! | |
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) { | |
if (ice && ice.candidate && ice.candidate.candidate) { | |
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]; | |
$('#showIP').append('Current IP : '+ myIP); | |
pc.onicecandidate = noop; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment