Skip to content

Instantly share code, notes, and snippets.

@CodyKochmann
Created May 13, 2015 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CodyKochmann/fb8033e14d73c2f35e14 to your computer and use it in GitHub Desktop.
Save CodyKochmann/fb8033e14d73c2f35e14 to your computer and use it in GitHub Desktop.
returns the users ip address in javascript
function myIP() {
// returns the users ip address
// ref link: http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript
if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","http://api.hostip.info/get_html.php",false);
xmlhttp.send();
hostipInfo = xmlhttp.responseText.split("\n");
for (i=0; hostipInfo.length >= i; i++) {
ipAddress = hostipInfo[i].split(":");
if ( ipAddress[0] == "IP" ) return ipAddress[1];
}
return false;
}
@Bhagi001
Copy link

Will it return the private IP address?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment