覆盖iframe的document.write [英] Overwriting iframe's document.write

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

问题描述

出于我自己的目的(咳嗽延迟加载广告脚本),我覆盖了document.write函数,以缓冲脚本的输出,将其写入div,并恢复本机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.

伪代码看起来像这样:


  • 保存原生代码document.write

  • 重新定义document.write

  • 脚本的eval和缓冲输出

  • 脚本完成后,将缓冲区写入

  • 恢复原生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,然后调用

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

frame.document.write

单步执行Firebug,我已经验证了这个document.write是原生的JS代码和我的覆盖版本。

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

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

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.

推荐答案

每个窗口都是一种独特而珍贵的雪花,因此带着创造者的某些礼物进入这个世界。这些礼物中有自己的窗口和文件物品,像黎明时代那样清澈透明的原始海洋。

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.

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

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.

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

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