mardi 9 octobre 2018

Javascript else section not triggering

Have a small javascript piece of code which calls navigator.geolocation.

I have an if statement, which works, to show the latitude and longitude if the user the user allows the call. However, the else section is not running.

If I run the code below I get prompted by the browser to allow; if I allow it then coordinates are shown. However, if I hit block instead nothing happens even though there is an else section.

<body>
  <br> <p id="ChangeSring">Test</p>
  <script>
  var x = document.getElementById("ChangeSring");
  if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(SavePosition);
  } else { 
      x.innerHTML = "Geolocation is not supported by this browser.";
      document.cookie = 'location=false;path=/form_handler';
  }

  function SavePosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
    document.cookie = 'location=true;path=/form_handler';
    document.cookie = 'latitude='+ position.coords.latitude +';path=/form_handler';
    document.cookie = 'longitude='+ position.coords.longitude +';path=/form_handler';
  }
  </script>

  <form action="/form_handler" _lpchecked="1">
    Enter value:
    <input type="text" name="SomeVar"><br>
    <input type="submit" value="search">
  </form>
</body>

Aucun commentaire:

Enregistrer un commentaire