code_junkie

Get Client IP using just Javascript?

Nov 13th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. <script type="application/javascript">
  2. function getip(json){
  3. alert(json.ip); // alerts the ip address
  4. }
  5. </script>
  6.  
  7. <script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>
  8.  
  9. function myIP() {
  10. if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
  11. else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  12.  
  13. xmlhttp.open("GET","http://api.hostip.info/get_html.php",false);
  14. xmlhttp.send();
  15.  
  16. hostipInfo = xmlhttp.responseText.split("n");
  17.  
  18. for (i=0; hostipInfo.length >= i; i++) {
  19. ipAddress = hostipInfo[i].split(":");
  20. if ( ipAddress[0] == "IP" ) return ipAddress[1];
  21. }
  22.  
  23. return false;
  24. }
  25.  
  26. <div style="display:none;visibility:hidden" id="uip"><%= Request.UserHostAddress %></div>
  27.  
  28. var ip = $get("uip").innerHTML;
Add Comment
Please, Sign In to add comment