I'm a beginner at javascript and pieced together the following code by searching online and lots of trial and error. I have simple data located in Google Fusion Tables and I would like to be able to pull data from the tables to use in equations. The only way I could get this to work was using JQuery/Ajax in non-async mode.
The code does what I want to do in non-Internet Explorer browsers but fails on MSIE8 (debugger shows that it gets stuck at the VsGM
variable within the TestJax
function).
Is there a way to get this work in IE? Am I totally on the wrong path, and if so is there a better way to retrieve this data (client side programming only)?
Here is my code:
<html>
<head>
<title>PSHA Output Page</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDTvatTw0U-rSCpen_FnOMMk0a5Pggb0Os&sensor=false">
</script>
<script>
function myNumspull(myx,myy,vstable) {
var query = "SELECT * FROM " + vstable+" WHERE Lat="+myy+" AND Lon="+myx;
var encodedQuery = encodeURIComponent(query);
var VsGM, Lon, Lat, rows;
// Construct the URL
var url = ['https://www.googleapis.com/fusiontables/v1/query'];
url.push('?sql=' + encodedQuery);
url.push('&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ');
var myJax = TestJax(url, callback);
return myJax;
}
var callback = function(data, textStatus, xhr)
{
console.log ('from callback function '+myFunction(data, textStatus));
myFunction(data, textStatus);
}
var TestJax = function(url, cb) {
var data = 'Input values';
$.ajax({
url: url.join(''),
async: false,
cache: false,
dataType: 'text',
success: cb
});
console.log(VsGM,isNaN(VsGM));
return VsGM;
}
function myFunction(data, status) {
var rows = data.split(" ");
Lon = rows[18]; // Longitude
Lat = rows[21]; // Latitude
VsGM = rows[24]; //vs
console.log ('from myFunction'+ Lon, Lat, VsGM);
}
</script></head>
<body><script>
var VsTable = new Object();
VsTable['360'] = '1jN5wsiRuJwvK3dQA9ZK_5yW4r1NuhlzC-3jb9wo';
var x1 = -121.50;
var y1 = 38.50;
var k = '360';
var testnumber = myNumspull(x1,y1,VsTable[k]);
document.write ('results = '+testnumber);
</script></body></html>