Thursday 3 April 2014

Detect / Find Client IP Address using JavaScript & jQuery


In this short article I will explain how to detect / find the IP Address of the client / user machine using JavaScript & jQuery.


I have added an HTML <span> tag which will display the IP Address of the client machine. To get the IP Address I am making a JSON call to the Free Web Servicehttp://jsonip.appspot.com/ and I am passing the name of the callback function which will be called on completion of the request.
When the request is completed the IP address present in the response object is displayed in the HTML <span> tag


<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://www.telize.com/jsonip?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>


No comments:

Post a Comment