Sometime you need to save visitor's ip address using javascript or jquery.
jQuery handle JSONP so it's very easy to get client ip address by passing url formatted with the callback=? parameter with $.getJSON
request.
You can also use $.get
request to get ip address with data type jsonp.
- <html lang="en">
- <head>
- <title>How to get client ip address in javascript or jquery?</title>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
- </head>
- <body>
- <h1>Your Ip Address (jsonip.com) : </h1>
- <span id="ip"></span>
- <h1>Your Ip Address (ipinfo.com) : </h1>
- <span id="ip1"></span>
- <script type="text/javascript">
- $.getJSON("http://jsonip.com?callback=?", function (response) {
- $("#ip").text(response.ip);
- });
- $.get("http://ipinfo.io", function(response) {
- $("#ip1").text(response.ip);
- }, "jsonp");
- </script>
- </body>
- </html>