Display user's state by tracking their ip address

456 Views Asked by At

I would like to display the name of a user's state in my HTML example below.

I've read about various Javascript options, but have experienced no success with my solutions. Can you assist me with 2 things:

  1. How to properly track a user's IP address to identify their state
  2. How to add the user's state within the p element before the comma => must happen before the page renders
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Get User State & Display</title>
</head>
<body>
    <p><strong>If you would like to know more about stock options in , contact our team today!</strong></p>

</body>
</html>

Thank you!!

1

There are 1 best solutions below

2
Emre Koc On

This might help with your first question: How to get client's IP address using JavaScript?

For the second, you could add a span with an id and use that to set the state in:

// HTML

<p><strong>If you would like to know more about stock options in <span id="user-state"></span>, contact our team today!</strong></p>

// JavaScript

// After getting the IP and state
document.getElementById('user-state').innerText = 'state';