0

Possible Duplicate:
Get Client IP using just Javascript?

I am working on .net mvc 3 application. i just want to get the ip address of the client. How can i get client ip using JavaScript. If any body knows please share .

Community
  • 1
  • 1
Nithesh Narayanan
  • 10,227
  • 32
  • 91
  • 134
  • Read the following link [an-i-lookup-the-ip-address-of-a-hostname-from-javascript][1] [1]: http://stackoverflow.com/questions/102605/can-i-lookup-the-ip-address-of-a-hostname-from-javascript – Ram Singh Jul 19 '12 at 06:29
  • JavaScript is client side scripting so it does not have access to Request object. Users request are being captured by the servers, so only server side scripts have access to it. In .NET you have property that gets the ip of client: Request.UserHostAddress –  Jul 19 '12 at 06:48

3 Answers3

3

You can't do that with javascript. You could use javascript to send an AJAX request to a controller action that will return the IP of the client reading it from Request.UserHostAddress:

public ActionResult GetIP()
{
    return Json(new { ip = Request.UserHostAddress }, JsonRequestBehavior.AllowGet);
}

and then:

var url = '@Url.Action("GetIP", "SomeController")';
$.getJSON(url, function(result) {
    alert(result.ip);
});
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
0
<html xmlns="http://www.w3.org/1999/xhtml">
              <head>
              <title></title>
              <script type="text/javascript">
                  window.onload = function () {
                      var script = document.createElement("script");
                      script.type = "text/javascript";
                      script.src = "http://jsonip.appspot.com/?callback=DisplayIP";
                      document.getElementsByTagName("head")[0].appendChild(script);
                  };
                  function DisplayIP(response) {
                      document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
                  }
              </script>
              </head>
              <body>
                  <form>
                      <span id = "ipaddress"></span>
                  </form>
              </body>
              </html>
Nirmal
  • 4,739
  • 13
  • 70
  • 113
  • More often then not, that will give the IP address of a router that performs NAT and not the client itself. Since ASP.NET is available, that IP address could be acquired from the request to the ASP.NET server, so it probably isn't what the OP is looking for. – Quentin Jul 19 '12 at 06:27
0
<!-- Require jQuery / Anyversion --><script type="text/javascript"  language="Javascript"    src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- Require EasyJQuery After JQuery --><script type="text/javascript"     language="Javascript" src="http://api.easyjquery.com/easyjquery.js"></script>
<script type="text/javascript" language="Javascript">
// 1. Your Data Here
function my_callback(json) {
    alert("IP :" + json.IP + " nCOUNTRY: " + json.COUNTRY);
}

function my_callback2(json) {
    // more information at http://api.easyjquery.com/test/demo-ip.php
    alert("IP :" + json.IP + " nCOUNTRY: " + json.COUNTRY + " City: " + json.cityName + " regionName: " + json.regionName);
}

  // 2. Setup Callback Function
  // EasyjQuery_Get_IP("my_callback"); // fastest version
 EasyjQuery_Get_IP("my_callback2","full"); // full version
</script>​

a working Example

Evan Lévesque
  • 2,721
  • 6
  • 35
  • 58