What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

How call script on load html instead of click event?

soogii

New member
Good day mates, I couldn't solve my issue mentioned in title. Maybe it is very easy for you.
My problem is the working code including a button but I don't need a button I want to be work it on .html on load. How could I make it?
HTML:
<!doctype html>
<html lang="en" class="no-js">

    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

        <title>Send your location</title>
 <!-- Meta Information -->
 <meta charset="utf-8" />
 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>

<button id="find-me">Show my location</button><br />
<p id="status"></p>
<a id="map-link" target="_blank"></a>

<body>
<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/search?query=${latitude},${longitude}`;

    mapLink.textContent = `Send your location - ???????`;
  }
 
  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);
//Here I want to make it work withoud click event
</script>
</body>
</html>
 
Back
Top