Overwriting iframe's document.write

南笙酒味 提交于 2019-12-07 16:11:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2256147/overwriting-iframes-document-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!