1fetch('https://api.ipify.org/?format=json')
2 .then(response => response.json())
3// Returns
4{
5 "ip": "user_ip"
6}
1// You can't extract the the public ip from with in the browser
2// but you CAN use a third party, there are a lot of them out there
3// I use www.ipify.org/
4
5// because, according to them:
6//
7// 1. You can use it without limit (even if you're doing millions of
8// requests per minute).
9// 2. ipify is completely open source (check out the GitHub repository).
10
11//Here's a WORKING JS EXAMPLE:
12
13<script>
14function getIP(json) {
15 alert("My public IP address is: " + json.ip);
16}
17</script>
18<script src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
19
20// as seperate script tag
21<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"> </script>
22
23
1<script type="application/javascript">
2 function getIP(json) {
3 document.write("My public IP address is: ", json.ip);
4 }
5</script>
6
7<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>