简体   繁体   中英

How to show the content of object on IE9

var obj = {'test1':"value1", "test2": "value2" }
console.log(obj);

reuslt

[object, Object]

Is is possible to show the content the object not to use JSON.stringify on IE9?
like the following

{'test1':"value1", "test2": "value2" }

Try using console.dir() instead of console.log() - this will also work with other browsers' consoles.

See also the MSDN article Internet Explorer 9 Developer Tools Deep Dive – Part 3: Debugging JavaScript .

If you just want to see what's in the complex object without using full scale library, you can use such code:

var complexObject = { 
    "first field": "first value", "second": function() {
        alert("hello");
    }, 
    "third": ["I", "am", "an", "array"]
};

var complexObjectString = "";
for (var key in complexObject) {
    complexObjectString += key + ": " + complexObject[key] + "\n";
}
alert(complexObjectString);​

Live test case .

In IE9 you need to be in standards mode to use the JSON object; eg, you must have a doctype.

This does not work: Live copy | source

<html>
<head>
<meta charset=utf-8 />
<title>Test Page</title>
</head>
<body>
<p>Without a doctype</p>
<script>
(function() {

  try {
    var obj = {
      foo: "bar"
    };

    var str = JSON.stringify(obj);

    display("<code>" + str + "</code>");
  }
  catch (e) {
    display("Exception: " + (e.message || e.toString()));
  }

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }
})();
</script>
</body>
</html>

It fails with an error that JSON is undefined.

If you add

<!doctype html>

to the top, it works: Live copy | source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM