How to get client IP address using jQuery

I want to know how to get client IP address using jQuery?

Is it possible? I know pure javascript can't, but got some code using JSONP from Stack Overflow itself.

So, is there any workaround using jQuery?



jQuery can handle JSONP, just pass an url formatted with the callback=? parameter to the $.getJSON method, for example:

$.getJSON("", function(e) {
<script src=""></script>

This example is of a really simple JSONP service implemented on with

If you aren't looking for a cross-domain solution the script can be simplified even more, since you don't need the callback parameter, and you return pure JSON.


A simple AJAX call to your server, and then the serverside logic to get the ip address should do the trick.

$.getJSON('getip.php', function(data){
  alert('Your ip is: ' +  data.ip);

Then in php you might do:

/* getip.php */
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

if (!empty($_SERVER['HTTP_CLIENT_IP']))
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
print json_encode(array('ip' => $ip));

<html lang="en">
    <title>Jquery - get ip address</title>
    <script type="text/javascript" src="//"></script>

<h1>Your Ip Address : <span class="ip"></span></h1>

<script type="text/javascript">
    $.getJSON("", function (data) {

function GetUserIP(){
  var ret_ip;
  $.ajaxSetup({async: false});
  $.get('', function(r){ 
    ret_ip = r.ip; 
  return ret_ip;

If you want to use the IP and assign it to a variable, Try this. Just call GetUserIP()


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.