插入< script>使用jQuery进入iframe [英] Inserting <script> into iframe with jQuery

查看:103
本文介绍了插入< script>使用jQuery进入iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下内容通过jQuery将代码段插入iframe:

I'm attempting to insert a snippet into an iframe through jQuery with the following:

var snippet = "<script>document.writeln('test')</script>";

jQuery('<iframe />').appendTo('body').append(snippet);

而不是在iframe中写test,它会覆盖父窗口。

instead of writing "test" in the iframe, it overwrites the parent window.

如何让父窗口获取插入了test的iframe?

How can I make it so that the parent window gets the iframe with "test" inserted in it?

推荐答案

如果你想控制它们,你应该将新窗口和iframe的源设置为'about:blank'。然后通过iframe的ID引用它!

You should set the source of new windows and iframes to 'about:blank' if you want control over them. Then reference it by the iframe's ID!

您还想使用 iframe.contentDocument || iframe.contentWindow.document

在你写'()之前'打开()'文件可能是个好主意。

You also want to use iframe.contentDocument || iframe.contentWindow.document
And it might be a good idea to 'open()' the document before you 'write()' to it.

更新:忘了这个:如果你打开窗口关于:空白,它需要时间加载..
所以你不能立刻写信给它! !
所以要么设置超时大约50ms(通常),然后写入新窗口/ iframe。

或者检查它是否已加载(onload),然后让它写入源(我更喜欢)这个)。

如果你需要重用iframe,将它设置为about:blank first(再次)并等待或再次检查onload再次写入帧之前。所有这一切都归功于xss安全性。

我也强烈建议使用传统的事件模型(因此没有addEvent的东西,它们不能像预期的crossbrowser一样工作并导致IE中的memoryleaks)。

Update: forgot this: If you open the window about:blank, it needs time to load.. So you cannot write to it right away!! So either set timeout of about 50ms (usually) and then write to the new window/iframe.
OR check if it is loaded (onload), then have it write the source (I prefer this).
If you need to reuse the iframe, set it to about:blank first (again) and wait or check onload again before writing to the frame again. All this is due to xss security.
I also strongly advise to use the traditional event-model (so no addEvent things, they don't work as intended crossbrowser and lead to memoryleaks in IE).

所以:创建iframe并将src设置为about:blank,添加一个onload函数来检查包含你的html-string的var:如果它是空的,否则:写吧到iframe并清空字符串。

更新iframe:将内容设置为包含html-string的var,然后将iframe的源设置为about:blank。

了解循环?

So: create iframe with src set to about:blank, add a onload function that checks a var containing your html-string: if it is empty, else: write it to the iframe and empty the string.
To update the iframe: set content to the var containing the html-string, followed by setting the source of the iframe to about:blank.
Understand the loop?

这个宝宝甚至适用于IE6 ......

This baby even works in IE6...

祝你好运!!

更新:你没有正确地逃避你的代码片段:应该是:< script> document.writeln('test')\< \ / script>

请参阅: jsfiddle

UPDATE: you did not escape your snippet properly: should be: <script>document.writeln('test')\<\/script>
See: jsfiddle

UPDATE2:argl ..我通常永不放弃,但因为我不关心jQuery,所以我不会通过jQuery的手册来获得一些如此简单的东西。像jQuery一样困难(抱歉)。出于现代(蹩脚)的安全原因,您需要一个:空白。期。

UPDATE2: argl.. I normally never give up, but since I don't care for jQuery, I'm not going through the manual for jQuery for something something so simple in something as difficult as jQuery (sorry). For modern (crappy) security reasons you need an about:blank. Period.

请参阅此更新的小提琴简单的'基础工作,然后看看如何在jquery中做,并做出选择:onload或setTimeout。我正在使用我自己的crossbrowser html-editor,这个主题需要花费一周的时间来搞清楚。

See this updated fiddle for the 'plain jane' basics at work, then see how to do it in jquery and make a choice: onload or setTimeout. I'm working on my own crossbrowser html-editor and this subject took over a week to figure out.

这篇关于插入&lt; script&gt;使用jQuery进入iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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