将 JavaScript 添加到 iframe 中?
我想知道是否可以将 javascript 添加到页面上的 iframe 或框架集?
请注意:内容来自同一域 www.domain.com
但子域不同 www.blah.domain.com 与 www.blah2.domain.com
,我不确定这是否重要。
I want to know if it is possible to add a javascript <script type="text/javascript">blahblah</script>
to an iframe OR frameset that is on the page?
Just a note: the content comes from the same domain www.domain.com
but the subdomain is different www.blah.domain.com vs www.blah2.domain.com
, I'm not sure if that matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这确实很重要,但幸运的是有一个相当简单的修复方法。您只需将两侧的
document.domain
属性设置为同一个域(例如,document.domain = 'domain.com'
)document.domain
只能设置为同一个超级域,因此您无法对www.example.com
和blah.example2.com
执行此操作。设置了
document.domain
属性后,您应该能够以与位于同一子域时相同的方式连接 iframe。以下是如何将脚本标记插入 iFrame 的方法:将脚本插入 iFrame 的标头,而不清除 iFrame 的主体
It does matter but luckily there's a fairly simple fix. You just need to set the
document.domain
property on both sides to the same domain (e.g.,document.domain = 'domain.com'
)document.domain
can only be set to the same super domain so you couldn't do this withwww.example.com
andblah.example2.com
.Once you have the
document.domain
property set you should be able to interface the iframe the same way you would if they were on the same subdomain.Here's how to insert the script tag into the iFrame: Insert a Script into a iFrame's Header, without clearing out the body of the iFrame