允许 iframe 链接跨域定位父框架 [英] Allow iframe links to target parent frames cross domain

查看:35
本文介绍了允许 iframe 链接跨域定位父框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个域.domain1 上的一个页面使用 iframe 从 domain2 加载内容.如何允许域 2(iframe 内)的链接在域 1 的完整父框架中打开?

I have 2 domains. One page on domain1 uses an iframe to load content from domain2. How do I allow links from domain2 (inside the iframe) open in the full parent frame on domain1?

我一直在查看 IE sandbox 属性 但我不确定如何实现这些,如果它们兼容跨浏览器.有没有其他人可以解决这个问题?

I've been looking at IEs application attribute and w3c's new sandbox attribute but I'm unsure how to implement these and if they're compatible cross browser. has anyone any other tricks to get around this?

我可以用 javascript 像素做些什么吗?

Is there something I can do with a javascript pixel maybe?

推荐答案

您需要使用 postMessage() 用于跨域发送数据.

You need to use postMessage() for sending data across domains.

1.编码 domain2.com 上的链接(iframe 内的链接):

<a href="javascript:;" onclick="top.postMessage('newpage.html','http://domain2.com')"></a>

2.捕获 domain1.com(父页面)页面头部的消息并导航:

window.addEventListener( "message", function(e){
    window.location.href = e.data  //navigates to newpage.html
}, false)

类似问题此处,如果您需要更多详细信息.对于 在这个页面上看起来很全面.

Similar issue here if you need more detais. For < IE8 and other old browsers, there are some hacky workarounds on this page which look quite comprehensive.

这篇关于允许 iframe 链接跨域定位父框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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