Search code examples
javascriptjqueryinternet-explorer

'length' is null or not an object? IE 8


I get the following error in IE8:

length is null or not an object

Anyone have any ideas? Feedback greatly appreciated.

function refresh() {
  $.getJSON(files+"handler.php?action=view&load=update&time="+lastTimeInterval+"&username="+username+"&topic_id="+topic_id+"&t=" + (new Date()), function(json) {
    if(json.length) {
      for(i=0; i < json.length; i++) {
        $('#list').prepend(prepare(json[i]));
        $('#list-' + count).fadeIn(1500);
      }
      var j = i-1;
      lastTimeInterval = json[j].timestamp;
    }
  });
}

Solution

  • Just check for the object being null or empty:

    if (json && json.length) {
      // ...
    }
    

    C'mon gang this was glaringly obvious :-)