There is a better solution ...
This does not give a direct answer to your question, but provides a complete solution to your problem.
The jquery-json library provides a wrapper that uses the built-in implementation of the JSON object if it is available and crashes back to its own JSON implementation if it is not. This will work in any browser.
Here is a usage example on the project home page:
var thing = {plugin: 'jquery-json', version: 2.3}; var encoded = $.toJSON( thing ); // '{"plugin":"jquery-json","version":2.3}' var name = $.evalJSON( encoded ).plugin; // "jquery-json" var version = $.evalJSON(encoded).version; // 2.3
Usage is very simple: toJSON builds a JS source; evalJSON converts JSON string data back to JavaScript objects.
You look at the source, the implementation is surprisingly simple, but it works very well. I used it personally in several projects.
There is no need to perform browser detection if it works in every browser.
Evan Plaice Oct. 16 2018-12-12T00: 00Z
source share