Here’s a simple program in JavaScript that can help you find the IP address of the device running the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<html> <head> <title>Find your IP Address using JavaScript</title> <script> // using api.ipify.org to get the public ip address // using JavaScript Fetch API to perform HTTP Request fetch('https://api.ipify.org?format=json') .then(response => response.json()) .then(data => document.getElementById("response").innerHTML = data.ip) .catch(error => console.error(error)); </script> </head> <body> Your Public IP Address is: <span id="response"></span> </body> </html> |
This program uses the fetch
API to make a GET request to the https://api.ipify.org?format=json
endpoint, which returns the device’s IP address in JSON format. The response.json()
method extracts the JSON data from the response, and the data.ip
property is used to access the device’s IP address. Finally, the IP address is printed on the browser using document.getElementByID("response").innerHTML = data.ip
.
I hope this program helps you understand how to print public IP Addresses using JavaScript Fetch API.