使用javascript选择帧上下文 [英] Select frame context with javascript
问题描述
有没有办法用Javascript以编程方式选择框架上下文?假设当前网页中存在两个不同的框架,我需要将hello附加到第二个框架。问题是第二帧具有与当前网页不同的域。使用chrome开发人员工具,我可以简单地选择第二帧上下文并从控制台执行 $('body')。append('hello')
但我应该能够以编程方式执行此操作。
Is there a way to programmatically select frame context with Javascript? Let's say there are two different frames present in the current web page, and I need to append 'hello' to the second frame. The problem is that the second frame has a different domain than the current web page. With chrome developer tools, I can simply choose the second frame context and do $('body').append('hello')
from console, but I should be able to do this programatically.
有没有办法实现这一目标?
Is there a way to achieve this?
意图:我需要创建一个专门针对具有与主网页不同的域的框架的书签。
intention: I need to create a bookmarklet that specifically targets the frame that has a different domain than the main webpage.
推荐答案
同一个域很简单:
window.parent.doSomeThing()
交叉起源有点棘手
这里是一篇很好的文章。
如果html5是一个选项:您可以使用 postMessage API
If html5 is an option: You can use the postMessage API
大多数浏览器现在已经获得支持:
Most browsers already have support now:
此处您可以了解您所定位的浏览器是否受支持。
Here you can find out if the browsers you target are supported.
这篇关于使用javascript选择帧上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!