I want to get Javascript data in another page and use it in an existing javascript.
Let me explain it with the codes.
I want to get this data from that other page
<script type="text/javascript">
var data = {
"users": [
{
"latitude": "48.405163",
"longitude": "2.684659"
},
{
"latitude": "43.7347242529278",
"longitude": "7.42198348045349"
}
]
};
</script>
I tried to get it it using this code
$(function MapData() {
$.getScript({
type: "GET",
url: "default.cs.asp?Process=ViewCheckinMap",
success: function(data) {
$("#content").append(data);
},
error: function (data) {
$("#content").append(data);
}
});
});
In order to use data inside the data variable it in this code
function initialize() {
var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
for (var i = 0; i < data.users.length; i++) {
var location = data.users[i];
var latLng = new google.maps.LatLng(location.latitude,
location.longitude);
var marker = new google.maps.Marker({
position: latLng
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
}
google.maps.event.addDomListener(window, 'load', initialize);
But I am getting data is not defined error.
I also tried getting data from the other page using
$.getScript('default.cs.asp?Process=ViewCheckinMap');
But I got the same error.
How can I fix this?
Many thanks!
When you’re loading javascript with
$.getScript(), you should not include<script>tags in the file you’re loading. Also, you need to run yourinitialize()function after you’ve finished loading the file: