So I have two <script>s.
From my understanding, I can place them into an external.js file by just copying and pasting the code.
I am creating a Google API:
<script>
function initMap() {
var uluru = {
lat: -25.363,
lng: 131.044
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
I basically want to move both of these into a file called map.js. I moved the first script in as below:
function initMap() {
var uluru = {
lat: -25.363,
lng: 131.044
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
And linking this new file and keeping the second script in the HTML format. It still worked. However, when I moved the second script into the file also:
function initMap() {
var uluru = {
lat: -25.363,
lng: 131.044
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
It didn’t work anymore. What did I do wrong?
<script>tag.https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMapand include that in your external script, but I wouldn't do that, it is on a content delivery network, and that's fine.