Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body onload="init();">
  <h1>Exemple simple</H1>
  Ma position : <span id="myPosition"></span>
  <p>
  <button id="myButton">Cliquer pour avoir la position</button>
  </p>
</body>
</html>
 
// TOUJOURS METTRE VAR
var displayCoord, myButton;
function init() {
  console.log("page chargée");
  
  // On récupère les objets du DOM
  myButton = document.querySelector("#myButton");
  // On définit les écouteurs
  myButton.addEventListener('click', getLocation);
  displayCoords=document.getElementById("myPosition");
  
  // On démarre le traitement si un traitement doit être fait dès
  // le chargement de la page
}
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    displayCoords.innerHTML="Geolocation API not supported !";
  }
}
function showPosition(position) {
  displayCoords.innerHTML="Latitude: " + position.coords.latitude +
"<br />Longitude: " + position.coords.longitude;
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
micbuffapro
0viewers