JavaScript program to retrieve clients IP address

JavascriptWeb DevelopmentObject Oriented Programming

Following is the code for retrieving clients IP address −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .result {
      font-weight: 500;
      font-size: 18px;
      color: blueviolet;
   }
</style>
</head>
<body>
<h1>Retrieve clients IP address</h1>
<div class="result"></div>
<script type="application/javascript">
   let resEle = document.querySelector(".result");
   function showIP(json) {
      resEle.innerHTML = "My public IP address is: " + json.ip;
   }
</script>
<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=showIP"></script>
</body>
</html>

Output

raja
Published on 16-Jul-2020 13:37:25
Advertisements