我对如何运行javascript代码一无所知

时间:2019-03-13 17:53:27

标签: javascript

function showPosition(position) {
  var latlon = position.coords.latitude + "," + position.coords.longitude;
  var img_url = "http://maps.googleapis.com/maps/api/staticmap?center=" +
    latlon + "&zoom=14&size=400x300&sensor=false";
  document.getElementById("html.net").innerHTML = "<img src='" + img_url + "'>";
}

如果在html文件中,如何在浏览器上运行此代码?否则,还有其他方法吗?

1 个答案:

答案 0 :(得分:-1)

将其放入HTML文件并将其另存为file.html或您要命名的任何名称

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>My First HTML Page</title>
</head>

<body>
  Check out this map
  <div id="output"></div>

  <script>
    function showPosition(position) {
      var latlon = position.coords.latitude + "," + position.coords.longitude;
      var img_url = "http://maps.googleapis.com/maps/api/staticmap?center=" +
        latlon + "&zoom=14&size=400x300&sensor=false";
      document.getElementById("output").innerHTML = "<img src='" + img_url + "'>";
    }
    //now we run the code
    showPosition({
      coords: {
        lattitude: 12345,
        longitude: 12345
      }
    })
  </script>
</body>

</html>