0

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>
BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

0

It is likely because of the console.log() call.

In IE, you have to remove or comment out console.log() lines or many times it will not process. log() is a method of the 'console' object, and in IE the console object is not defined until the Developer Tools have been opened (press F12 to open the tools).

If you have Developer Tools open before the JavaScript is executed then it won't have this problem. Additionally, you can define a dummy/fallback console.log() object and method for the JavaScript to use when the browser does not currently have a console object defined (when the Developer Tools aren't open). See this StackOverflow question for some code: What happened to console.log in IE8?

Community
  • 1
  • 1
Ryan
  • 3,153
  • 2
  • 23
  • 35
  • When I remove the all the console.log()s, IE still will not run the script. Debugger gets stuck at the "return VsGM;" line in the TestJax function. – user2348554 May 06 '13 at 18:08
0

I understand that this is an old question but I had the same problem in Internet Explorer 8 and 9 so I'm sharing the solution.

The ajax returned an error in IE8 and IE9. This is because IE tried to download the file instead of simply reading it.

So in your ajax request, you simply need to specify the dataType which in this case is jsonp.

$.ajax({
  type: "GET",
  async: false,
  cache: false,
  dataType: 'jsonp',
  url: query,
  success: function(e) { 
    //...       
  },
  error : function(e) {
    //...        
  } 
});
VVV
  • 7,563
  • 3
  • 34
  • 55