I’ve got this location script:
<html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<p id="demo"> </p>
<script>
var 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;
}
window.onload = getLocation();
</script>
</body>
</html>
and it works perfect but what I want to do is to display the coordinaes in my body one more time without copy the same script. I just want something like php code echo $coords for example?
Stop using
xas a global. Make it an argument of getLocation() (and give it a more intention revealing name).showPositiononly gets called fromgetLocation, define it insidegetLocation, then it can use the same variable.