restore overridden window.JSON object

后端 未结 2 614
夕颜
夕颜 2021-01-18 06:27

Some code that I don\'t have control over is overriding the global JSON object without checking if it\'s already implemented:

var JSON = {
  org: \"http://ww         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 07:14

    Since the code that you don't have control over is overriding the original before you come along in the page, you have two options:

    Inject an iframe and grab the JSON off the contextWindow (as indicated in the other answer on this question at the time of this edit), or, alternately, just use the https://github.com/douglascrockford/JSON-js JSON library as your own insert. Note that using Crockford's does give cross-browser-guarantees of conformance, but the native implementations are often faster.

    An alternative if you have the ability in the future to come along on the page before the offending code, is to inject something before that offending "code that helps" to grab the JSON object:

    
      
        
     ....
    

提交回复
热议问题