0

I am getting this strange error even when parseXml is defined. The piece of code works fine in Chrome but NOT in Firefox.

   $(document).on("pageinit", "#map-page", function () {
       var defaultLatLng = new google.maps.LatLng(56.8517843, 14.828458); // Default somewhere to Växjö when no geolocation support
       if (navigator.geolocation) {
           var stations = [];
           $.ajax({
               type: "GET",
               url: "busstations.xml",
               dataType: "xml",
               success: parseXml
           });

           function parseXml(xml) {
               $(xml).find('station').each(function () {
                   var name = $(this).find("name").text();
                   var localurl = $(this).find("localurl").text();
                   var latitude = $(this).find("latitude").text();
                   var longitude = $(this).find("longitude").text();
                   navigator.geolocation.getCurrentPosition(success, fail, {
                       maximumAge: 500000,
                       enableHighAccuracy: true,
                       timeout: 6000
                   });

                   function success(pos) {
                       currentLatitude = pos.coords.latitude;
                       currentLongitude = pos.coords.longitude;
                       console.log(pos.coords.latitude + " " + pos.coords.longitude);

                   }

                   function fail(error) {
                       alert("No GL support!");
                   }

                   stations.push({
                       "name": name,
                       "localurl": localurl
                   });
                   console.log(JSON.stringify(stations));


               });
           }
       }
   });

However if I remove the if(navigator.geolocation) check condition on the 3rd line, then it also works fine in Firefox and there is also no such undefined ReferenceError.

Also if I bring this if(navigator.geolocation) check condition inside the parseXml function, the code works fine. Wonder what is causing the problem in Firefox.

SASM
  • 1,292
  • 1
  • 22
  • 44

2 Answers2

1

Is this acceptable and working?

$(document).on("pageinit", "#map-page", function () {
   var defaultLatLng = new google.maps.LatLng(56.8517843, 14.828458); // Default somewhere to Växjö when no geolocation support
   if (navigator.geolocation) {
       $.ajax({
           type: "GET",
           url: "busstations.xml",
           dataType: "xml",
           success: parseXml
       });
   }
 });


function parseXml(xml) {
   var stations = [];
   $(xml).find('station').each(function () {
   var name = $(this).find("name").text();
   var localurl = $(this).find("localurl").text();
   var latitude = $(this).find("latitude").text();
   var longitude = $(this).find("longitude").text();
   navigator.geolocation.getCurrentPosition(
     function(pos) {
       currentLatitude = pos.coords.latitude;
       currentLongitude = pos.coords.longitude;
       console.log(pos.coords.latitude + " " + pos.coords.longitude);
     },
     function(error) {
      alert("No GL support!");
     }, 
     {
       maximumAge: 500000,
       enableHighAccuracy: true,
       timeout: 6000
     }
   );
   stations.push({
     "name": name,
     "localurl": localurl
   });
   console.log(JSON.stringify(stations));
 });
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Yes that worked perfectly. +1 for bringing `var stations = [];` inside the `parseXml` function as well. `Jonathan Naguin` answered it first in the comment. So I will take his answer. Thanks..:) – SASM Jul 04 '13 at 15:25
0

The problem might be that Firefox deals a little different with function declarations inside conditionals statments. The documentation says:

Note: Although this kind of function looks like a function declaration, it is actually an expression (or statement), since it is nested within another statement. See differences between function declarations and function expressions.

So if it is an expression then when the ajax call try to use it the function is not defined yet.

To fix it change the order of the declaration or declare the function outside.


This is covered also in this question.

Community
  • 1
  • 1
Jonathan Naguin
  • 14,526
  • 6
  • 46
  • 75