1

So I'm trying to troubleshoot this error on a client site, that only occurs on IOS 10 on mobile devices. They said it worked prior to upgrading their devices to IOS 10. Reverting back to IOS 9 actually doesn't produce this error. It appears to be something with JSON.parse along with it being localstorage data (this.local[i])). See below for js error, sample data, and javascript snippet.

Javascript Error:

[Error] SyntaxError: JSON Parse error: Unexpected identifier "function"
parse (Locus.js:40)

Data sample: (this.local)

[{"ObservationID":"444","Username":"blah","Deleted":0,"Flagged":0},
{"ObservationID":"555","Username":"blah","Deleted":0,"Flagged":0}]

Javascript:

Locus.prototype.loadFromLocal = function () {
    if (this.local) {
        for (var i in this.local) {
         var len = ('' + this.local[i]).split('{').length;
         if (len != 1) {
          this.data[i] = JSON.parse(this.local[i]);
         } 
         else {
             if (parseFloat(this.local[i]) == this.local[i]) {
                 /* local storage is a number */
                 this.data[i] = parseFloat(this.local[i]);
             } 
             else 
             {
                 /* already parsed */
                 this.data[i] = this.local[i];
             }
         }
    }
}
joby-flick
  • 1,790
  • 1
  • 12
  • 21

1 Answers1

1

I figured this out. It appears that IOS 10 will terminate javascript on error. The error I was getting was because my object (this.local) had other items in it that weren't of the same type. So the first item was the json string above, but the second item was a number. When it tried to perform a split() on the second item, it bombed. What's interesting is that it doesn't error out on any of the common desktop/mac browsers and does what it's supposed to do.

The fix was to check the type of the item first, using typeof and a try catch to differentiate between a string and a json string.

typeof: http://www.javascriptkit.com/javatutors/determinevar2.shtml

json vs string: How to check if it's a string or json

if (typeof this.local[i] == 'number') 
{
    this.data[i] = parseFloat(this.local[i]);
}
else if (typeof this.local[i] == 'string')
{
    try
    {
        this.data[i] = JSON.parse(this.local[i]);
    }
    catch(e)
    {
        this.data[i] = this.local[i];
    }
}
Community
  • 1
  • 1
joby-flick
  • 1,790
  • 1
  • 12
  • 21