2016-07-07 13 views
0

私はhtml + javascriptを使用することを学んでいますが、私は非常に単純なコードで宿題に固執しています。 これは私のhtmlです:シンプルなjavascriptはロードされません

<!DOCTYPE html> 
    <html> 
     <head> 
      <meta charset="UTF-8" /> 
      <title> Geolocation and Local Storage and CSS3 </title> 
      <script type="text/javascript" src="my.js"></script> 
      <link rel="stylesheet" type="text/css" href="css.css" /> 
     </head> 

    <body> 
     <div> 
     Location: 
     <button type="button" onclick="getLocation()"> Get Location </button> 
     </div> 

     <div id="holder"> 
     </div> 
    </body> 
</html> 

、これは外部のJavaScriptです:私は、外部ファイルを取り除くと、ちょうどそれが作品の頭の中のスクリプトを入れて取得する場合

<script> 
var x = document.getElementById("holder"); 

function getLocation() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition); 
    } else { 
     x.innerHTML = "Geolocation is not supported by this browser."; 
    } 
} 

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("holder").innerHTML = "<img src='"+img_url+"'>"; 
} 
</script> 

奇妙な事があります私はパスが正しいと考えて、奇妙な外部ファイルをロードしていないと推測しています。

+8

奪って試してみてくださいjavascriptファイルの ' 'は含まれていないはずです。 – epascarello

答えて

3

2番目のファイルはJavascriptファイルではありません.Javascriptセクションを含むHTMLファイルです。

Eli Sadoffが言ったように<script>タグを取り除くと、すべてうまくいくでしょう。

1
  1. JavaScriptファイルでタグを削除します。
  2. HTMLとJavaScriptファイルが同じフォルダにあるかどうかを確認してください。
  3. JavaScriptファイルの名前を確認してください。これはHTMLで説明したのと同じですか?
0

<script>タグは、JavascriptをHTMLファイルに埋め込むためのものです。

  • 右このよう</body>

    の上ごJSリンクにあなたのリンクを貼る

  • を提出あなたのJavaScriptからスクリプトタグを削除します。

    <body> <div> Location: <button type="button" onclick="getLocation()"> Get Location </button> </div> <div id="holder"> </div> <script type="text/javascript" src="my.js"></script> </body>

関連する問題