How do I convert this piece of code into an alert that pops up after page has loaded?
I want to convert this button into an alert / message that pops up after the page has loaded.
I tried integrating it via “body onlaod” into the page code.
<body onload="geoFindMe()">
<article>
<h2>Geolocate</h2>
<button id="find-me">Show the location</button><br />
<p id="status"></p>
<a id="map-link" target="_blank"></a>
</main>
</article>
</div>
Here ist the script for the geolocation that I want to be
displayed in the alert.
<script>
function geoFindMe() {
const status = document.querySelector("#status");
const mapLink = document.querySelector("#map-link");
mapLink.href = "";
mapLink.textContent = "";
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
status.textContent = "";
mapLink.href = `https://www.openstreetmap.org/#map=18/${latitude}/${longitude}`;
mapLink.textContent = `Breitengrad: ${latitude} °, Längengrad: ${longitude} °`;
}
function error() {
status.textContent = "Unable to retrieve your location";
}
if (!navigator.geolocation) {
status.textContent = "Geolocation is not supported by your browser";
} else {
status.textContent = "Locating…";
navigator.geolocation.getCurrentPosition(success, error);
}
}
document.querySelector("#find-me").addEventListener("click", geoFindMe);
</script>
</body>