0

I want to provide simple web service and let anyone to embed it in any site using JavaScript.

The Service is to provide Visitor IP address for Static Websites or any one that wants to used JavaScript for showing IP address instead of Server-Side script.

my code is:

<div style="white-space:normal; display:inline-block;background-color:white">
Your IP Address is: <br><strong>
{ip}</strong><br>Country:{country}<br>
ISP:{isp}
</div>

Screen Shot: enter image description here

I can use document.write() and output this content, but I am not sure if its the best choose, or using another method like appendChild to a parent element? last note, should I enclose the script between window.onload()?

Akam
  • 1,089
  • 16
  • 24

1 Answers1

0

You could use good old VanillaJS. By just giving it an ID, then changing its innerHTML.. Its basically Javascript DOM. It changes the actual html Learn More here.

document.getElementById("IP").innerHTML = "XXX.XXXXX.XXXX";
document.getElementById("Country").innerHTML= "U.S.A";
document.getElementById("ISP").innerHTML= "ISP Name";
<div style="white-space:normal; display:inline-block;background-color:white">
Your IP Address is: <span id="IP"></span><br>
Country:<span id="Country">{country}</span><br>
ISP:<span id="ISP">{isp}</span><br>
  
</div>

So i get the id by doing the usual document.getElementById.. Then you can change its innerHTML By doing .innerHTML.

EDIT
Here is a codepen!

EDIT2

So now i understood you want to do this using document.write(); Well its going to be kinda tedious but this is preety much the overall result..:

<h1>Representation</h1>
<div style="white-space:normal; display:inline-block;background-color:white">
  Your IP Address is: <span id="IP">
<script>
document.write("XXX.XXXXX.XXXX");
</script></span>
  <br>Country:
  <span id="Country"><script>
document.write("U.S.A");
</script></span>
  <br>ISP:
  <span id="ISP"><script>
document.write("ISP Name");
</script></span>
  <br>

</div>
amanuel2
  • 4,508
  • 4
  • 36
  • 67