Piccolomomo
Piccolomomo

Reputation: 58

Object values undefined on IE

I've got this flow on an enterprise application:

  1. on every service page there is a "search box". When user starts to type something in it a call to a Struts 2 action in fired (via JQuery autocompleter)
  2. on action call, Hibernate looks for hints on user search on a MySql database
  3. when hints are found a JSON response is given. Something like:

    ...
    "map": {
          "id": "1234",
          "title": "Title 1",
          "permalink": "title-1",
          "class": "contentClassA",
          "contentType": "Content type ..."
        },
    ...
    
  4. back on frontend JSP, some Javascript creates a list ov <div>, each of them containg the data of a map object.

Steps 1-4 is working on Firefox up to version 11 and Internet Explorer to version 9. Then via Javascript I try to build a self.location redirect to reload the page discriminating on the class value. And here is the problem. The obj variable contains a single JSON map as stated above, and I do:

    var classType; 
    if(obj['class'] != undefined) {
        classType = obj['class'];
    } else {
        //classType = obj.map.class;
        //classType = obj['map'].class;
        //classType = obj.class;
        // ...
        classType = obj.map['class'];
    }

Everything is ok on FF, but IE (ver. 7-8-9) falls into the else and the classType variable returned is undefined no matter what I try.

Am I missing something?

Upvotes: 1

Views: 2659

Answers (2)

David Hellsing
David Hellsing

Reputation: 108500

I wonder what purpose you have with this logic. But, if you want to check if obj.map has a class key, use hasOwnProperty:

if (obj.map.hasOwnProperty('class')) {
    // do something with obj.map['class']
}

If you don’t know if map is present in obj you can check for that too:

if ('map' in obj && obj.map.hasOwnProperty('class')) {
    //
}

There are many ways to check if properties exists in an object, all depending on how much control you have of the data and what cases you need to account for.

Upvotes: 0

Nemoy
Nemoy

Reputation: 3417

I think your issue is with propertyName "class", which is a reserved keyword in js. Check below link.

http://javascript.about.com/library/blreserved.htm

Even I faced similar issue with IE, I simply renamed the key class to className

Upvotes: 1

Related Questions