skip to Main Content

I searched many snippets to print my ip address in my browser without backend code using only javascript, but none of it works.

I am trying to capture user IP address on my webpage so I can determine whether the IP address lies within a IP block to determine user location.

2

Answers


  1. <!DOCTYPE html>
    <html>
    <body>
    <h1>HTML Geolocation</h1>
    <button onclick="getLocation()">Get Location</button>
    
    <p id="demo"></p>
    
    <script>
    const x = document.getElementById("demo");
    
    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
      }
    }
    
    function showPosition(position) {
      x.innerHTML = "Latitude: " + position.coords.latitude + 
      "<br>Longitude: " + position.coords.longitude;
    }
    </script>
    
    </body>
    </html>
    

    you can use the above code to get the current Longitude and latitude.

    second step you can use google api to get live location as pass parameters Longitude and latitude.

    easily you can get live location.

    Login or Signup to reply.
  2. As far as I know, you cannot recognize the IP address in the web part of your site.

    Only the backend can now have the user’s IP address.

    I think you can write a simple backend with one endpoint and send one request to get the user’s IP address.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search