覆盖iframe的document.write
出于我自己的目的(咳嗽延迟加载广告脚本),我覆盖了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每扇窗户都是独特而珍贵的雪花,因此是带着造物主的某些礼物来到这个世界的。在这些礼物中,有它自己的“窗口”和“文件”物品,就像黎明时分水晶般清澈的原始海洋一样清新纯净。
类似地,所有基本的 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.