2

I have javascript object which i need to convert into string.

var obj = {"name": "XXX", "age": "27"};

After googling i have got JSON.stringify(obj);

JSON.stringify(obj); is working fine if IE8 modes are same as below

Browser Mode : IE8
Documentn Mode: IE8 Standards

and the same code is not working If

Browser Mode : IE8
Documentn Mode: Quirks Mode

I am wondering why same thing is not working...

any idea will be appreciated.

Dairo
  • 822
  • 1
  • 9
  • 22
  • 1
    Lots of things don't work in old versions of Internet Explorer. This is one of them. Don't make quirks mode pages. – Pointy Jan 16 '14 at 20:59
  • @Pointy but why this is not working in quirks mode... this thing i want to understand... –  Jan 16 '14 at 21:02
  • It's because IE8 is old and broken and Microsoft simply made it that way. You can ask them if you want to know the reason. When IE goes into quirks mode it starts using old code, and it just doesn't support the JSON object. **You should not be creating new quirks mode pages anyway.** – Pointy Jan 16 '14 at 21:04
  • @Pointy Actually my existing application is opened in quirks mode by default... i am trying to get data using javascript.. –  Jan 16 '14 at 21:08
  • Put a proper `` in your application then. – Pointy Jan 16 '14 at 21:10
  • i cant change the existing code of the application... :( –  Jan 16 '14 at 21:13

1 Answers1

0

I would recommend using JSON.stringify if you can fixed IE modes to IE8 and IE8 standards. JSON.stringify will serializes an object and very easy to use. Most modern browsers support this method natively, but for those that don't, you can include a JS version

and If you can not fixed your IE modes then use below method to convert object to string.

Function:

function objToString (obj) {
var tabjson=[];
for (var p in obj) {
    if (obj.hasOwnProperty(p)) {
        tabjson.push('"'+p +'"'+ ':' + '"' +obj[p] + '"');
    }
}  tabjson.push()
return '{'+tabjson.join(',')+'}';
}

Call a function:

var obj = {"name": "XXX", "age": "27"};
objToString(obj );

Output:

"{"name":"XXX","age":"27"}"
SK.
  • 4,174
  • 4
  • 30
  • 48