-1

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!!

mash12
  • 1
  • 3
  • 1
    Does this answer your question? [How to get client's IP address using JavaScript?](https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript) – MathKimRobin Jul 31 '20 at 15:07

1 Answers1

0

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';
Emre Koc
  • 1,481
  • 10
  • 18
  • Thank you @Emre – after retrieving the IP and state, would you suggest adding that JavaScript in the head so that it is applied before the page is rendered? Or should I include it near the bottom of the body? – mash12 Jul 31 '20 at 16:57
  • Either at the top and with an onload, or at the bottom. Because if it runs before the span is rendered, it wont find it and cant set the text. – Emre Koc Aug 01 '20 at 04:30