覆盖iframe的document.write

发布于 2024-08-21 13:15:52 字数 627 浏览 2 评论 0原文

出于我自己的目的(咳嗽延迟加载广告脚本),我覆盖了 document.write 函数以缓冲脚本的输出,将其写入 div,并恢复本机 document.write当我完成时。

伪代码看起来像这样:

  • save off native code document.write
  • redefine document.write
  • eval and buffer output of script
  • When script is done, write buffer to
  • Restore native document.write

问题发生在粗体步骤 - 广告脚本中的其中一行创建了一个 iframe,并调用

frame.document.write

Stepping through Firebug,我已验证此 document.write 是本机 JS 代码,不是我覆盖的版本。

有没有办法覆盖所有窗口和框架的 document.write ?这是一个范围问题,但我不确定如何强制我的 document.write 占上风。

For my own purposes (cough lazy-loading an ad script), I am overwriting the document.write function in order to buffer the script's output, writing it to a div, and restoring the native document.write when I am done.

The pseudo-code looks something like this:

  • save off native code document.write
  • redefine document.write
  • eval and buffer output of script
  • when script is done, write buffer to
  • restore native document.write

The problem happens in the bolded step - one of the lines in the ad script creates an iframe, and calls

frame.document.write

Stepping through Firebug, I have verified that this document.write is the native JS code and not my overwritten version.

Is there a way to overwrite document.write for ALL windows and frames? This is a scoping issue, but I'm not sure how to force my document.write to prevail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落叶缤纷 2024-08-28 13:15:52

每扇窗户都是独特而珍贵的雪花,因此是带着造物主的某些礼物来到这个世界的。在这些礼物中,有它自己的“窗口”和“文件”物品,就像黎明时分水晶般清澈的原始海洋一样清新纯净。

类似地,所有基本的 Javascript“类”(函数、数组、RegExp 等)都是“干净的”。这就是为什么当您将页面加载到 iframe 中时,如果该页面有自己的依赖于(例如)jQuery 的本地脚本,则它必须导入自己的库副本。

Every window is a unique and precious snowflake, and as such is brought into this world with certain gifts from the Creator. Among those gifts are its own "window" and "document" objects, as fresh and pure as the crystal-clear primeval oceans at the Dawn of Time.

Similarly, all of the basic Javascript "classes" (Function, Array, RegExp, etc) are all "clean". That's why when you load a page into an iframe, if that page has its own local scripts that rely on (say) jQuery it has to import its own copy of the library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文