iframe 中包含的 JS 具有顶部框架窗口的上下文
$('<script/>', {
src: '/path/to/javascript.js',
type: 'text/javascript'
}).appendTo($('#iframe').contents().find('body'));
如果我错了,请纠正我,但我认为应该将 JS 加载到 iframe 中。我还尝试附加到head
。
问题
javascript.js 被执行,但该脚本中的 console.debug(this) 会返回顶部框架窗口。我试图验证该脚本是否确实包含在 iframe 中,但不知道如何验证。
此外,从 javascript.js< 运行
$('a')
/code> 返回顶部框架中的所有链接,而不是我想要的 iframe 中的每个链接。
感谢您抽出时间!
更新:我整理了一个独立的测试用例< /a> 您也可以下载 。检查控制台,注意这是顶部框架(可以通过变量 _TOP 进行验证)。
$('<script/>', {
src: '/path/to/javascript.js',
type: 'text/javascript'
}).appendTo($('#iframe').contents().find('body'));
Correct me if I'm wrong, but I think that should load the JS into the iframe. I've also tried appending to head
.
The problemjavascript.js
is executed, but console.debug(this)
in that script returns the top frame window. I've tried to verify that the script is actually included in the iframe, but don't really know how.
Additionally, running $('a')
from javascript.js
returns all links in the top frame, not every link in the iframe which I'd like.
Thanks for your time!
Update: I've put together an isolated test case which you also can download. Check the console and note that this is the top frame (can be verified by the variable _TOP).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个灰色地带。对于使用 jQuery 的此特定操作,在幕后您将使用
importNode
或adoptNode
,具体取决于浏览器。然而,IE 也不支持(因为我上次研究过)。您可能想要获取该文档的参考并编写脚本。如果我没记错的话:
This is kind of a grey area. For this specific action using jQuery, under the hood you're using
importNode
oradoptNode
depending on the browser. However, IE won't support either (since I last researched it).You might want to get a reference to the document, and write the script. If memory serves me right:
我能够在同一域 iframe 更新中进行一些操作:
http://jsfiddle.net/maniator/DX6bg/1/
确保这两个 URL位于同一域中。
(包括 from 中是否有
www
)更新
您可以像这样测试它:
现在,如果您看到文本
this is a test
在 iframe 中你知道它正在工作I was able to make something in the same domain iframe update:
http://jsfiddle.net/maniator/DX6bg/1/
Make sure that the two URLs are in the same domain.
(including if there is a
www
in from or not)UPDATE
You can test it like this:
now if you see the text
this is a test
in the iframe you know it is working