在跨域中获取iframe当前src网址 [英] Getting iframe current src url in cross domain

查看:918
本文介绍了在跨域中获取iframe当前src网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网络应用程序中有一个iframe,我需要从父文档获取其当前网址(当用户导航框架并更改原始源网址时)。

I have an iframe in my web app and I need to get its current url from the parent document (also when the user navigates the frame and change the original source url).

只需将网址社交分享即可。

Url is needed simply to social share it.

作为跨网域的场景,自己的子文档(它的远程域)。

As a cross-domain scenario, I don't own the child document (its a remote domain).

我知道同源的政策,防止从父文档到子文件的跨域访问,但我

I am aware of the same-origin-policy that prevents cross domain access from parent document to child one, but I'm looking for a solution by any creative mean possible - there has to be a way around.

推荐答案

如果你没有找到解决方案控制iFrame内容,那么唯一的解决方案是创建一个代理,将以下代码行注入每一页。

If you don't control the iFrame content, then the only solution is to create a proxy that injects the following line of code into every page.

window.postMessage(window.location,'*');

然后在父页面上,您可以监听消息。

Then on the parent page you can listen for the message.

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event) {
  console.log(event.data);
}

这篇关于在跨域中获取iframe当前src网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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