I’m working with a location picker script that sends coordinates to a backend database. The application runs perfectly in Chrome and Internet Explorer, but Firefox causes issues. When I click the save button in Firefox, it just reloads the page instead of saving the coordinates. I’m not very experienced with JavaScript debugging, so any help would be great!
My Code:
<!DOCTYPE html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Location Picker Map</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
var mapMarker;
var popup;
function initMap() {
var coords = new google.maps.LatLng(40.7128, -74.0060);
var mapSettings = {
zoom: 2,
center: coords,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var myMap = new google.maps.Map(document.getElementById('mapContainer'), mapSettings);
var popupContent = "<div><input type='button' value='Store Position' onclick='storeCoordinates(), window.parent.location.reload();'/></div>";
popup = new google.maps.InfoWindow({
content: popupContent
});
function placeMarker(pos) {
if (!mapMarker) {
mapMarker = new google.maps.Marker({
position: pos,
map: myMap
});
}
else { mapMarker.setPosition(pos); }
}
google.maps.event.addListener(myMap, 'click', function(e) {
placeMarker(e.latLng);
google.maps.event.addListener(mapMarker, 'click', function() {
popup.open(myMap, mapMarker);
});
});
}
function storeCoordinates() {
var position = mapMarker.getPosition();
var endpoint = 'save_coordinates.php?latitude=' + position.lat() + '&longitude=' + position.lng();
fetchData(endpoint, function(response, status) {
if (status == 200 && response.length >= 1) {
popup.close();
}
});
}
function fetchData(endpoint, callback) {
var xhr = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
xhr.onreadystatechange = emptyFunction;
callback(xhr.responseText, xhr.status);
}
};
xhr.open('GET', endpoint, true);
xhr.send(null);
}
function emptyFunction() {}
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initMap()">
<div id="mapContainer" style="width: 100%; height: 800px"></div>
<div id="status"></div>
</body>
</html>