浏览器没有获取javascript文件

时间:2017-06-18 19:09:39

标签: javascript html

我正在尝试使用Google Maps API,但我的HTML无法找到我的javascript文件。查看开发工具的网络选项卡,它甚至都没有尝试获取script.js文件。没有404或200响应。每个其他剧本都被发现完美无缺。

文件结构:

/css
  - styles.css
/js
  - script.js
index.html

index.html脚本:

<script async type="text/javascript"
 src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script
 src="https://code.jquery.com/jquery-3.2.1.min.js"
 integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
 crossorigin="anonymous"></script>
<script async type="text/javascript"
 src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script async type="text/javascript"
 href="js/script.js"></script>
<script async defer type="text/javascript"
 src="https://maps.googleapis.com/maps/api/js?v=3&callback=initMap"></script>

的script.js:

var map;
function initMap() {
    // Constructor creates new map
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: 40, lng: -70},
      zoom: 25
    });
}

2 个答案:

答案 0 :(得分:3)

您正在使用“HREF”

<script async type="text/javascript"
 href="js/script.js"></script>

你应该使用“SRC”

<script async type="text/javascript"
 src="js/script.js"></script>

答案 1 :(得分:-1)

您需要将script.js放在map.googleapis.js

之后

因为您正在调用使用Googleapis.js的表达式。