恢复被覆盖的窗口 [英] restore overidden window.JSON object

查看:103
本文介绍了恢复被覆盖的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法控制的一些代码覆盖全局JSON对象,而不检查它是否已实现:

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://www.JSON.org",
  copyright: "(c)2005 JSON.org",
  license: "http://www.crockford.com/JSON/license.html",
  stringify: function(a, g) {
     ...

问题是,这个版本的JSON解析器是非常旧的,有一个错误,这是困扰我的尝试在序列化。 (其他人在此实施方案中遇到了类似问题)。

The problem is that this version of the JSON parser is very old and has a bug, which is fouling up my attempts at serialization. (Others have had a similar problem with this implementation.)

我可以在浏览器的本地实现吗?我认为删除会工作,但它不会。我怀疑这是因为JSON是一个对象,而不是原型中的方法

Can I get at the browser's native implementation? I thought delete would work, but it doesn't. I suspect that's because JSON is an object and not a method in the prototype. Is there some other way to get at it?

推荐答案

您可以创建一个 iframe 元素(它将加载 about:blank ,因此创建一个新的上下文)并从中获取一个JSON对象。

You can create an iframe element (which will load about:blank and hence create a new context) and get a JSON object from there.

function restoreJSON() {
  var f = document.createElement("iframe");
  f.style.display = "none";
  document.documentElement.appendChild(f);
  window.JSON = f.contentWindow.JSON;
  document.documentElement.removeChild(f);
}

about:blank 是同步加载的,因此不需要等待加载事件。虽然这不是恢复原始的JSON对象,但它会得到一个黑盒子与它相同。

about:blank is loaded synchronously, so no need to wait for the load event. While this isn't restoring the original JSON object, it is getting one black-box identical to it.

这篇关于恢复被覆盖的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆