0

I'm trying to use the Google Maps API but my HTML cannot find my javascript file. Looking at the network tab of the dev tools it's not even trying to fetch the script.js file. No 404 or 200 response. Every other script is being found perfectly fine.

File Structure:

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

index.html scripts:

<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
    });
}
1
  • Press Ctrl+F5 Commented Jun 18, 2017 at 19:16

2 Answers 2

3

You are using "HREF"

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

You should use "SRC"

<script async type="text/javascript"
 src="js/script.js"></script>
Sign up to request clarification or add additional context in comments.

5 Comments

This can be your comment. Doesn't look like an answer.
Oh woops, thanks, guess I just needed a second set of eyes!
@Nimish This look exactly like an answer
@AlonEitan this can be just comment. Posted answer to gain reputation. Never mind.
@RenanSouza Accepted, had to wait the set time first!
-1

You need to put the script.js after the map.googleapis.js

Because your are calling an expression that use the Googleapis.js.

1 Comment

No. That would break it. The script defines a function. The Googleapis.js script will attempt to call that function. If you reverse the order, then the function won't exist in time and it will error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.