访问IFrame的父URL [英] Access IFrame's parent URL

查看:91
本文介绍了访问IFrame的父URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

从iframe访问父级网址

我一直在寻找但无法找到一个答案或解决方案。
我有两个网站, www2.domain.net www3.domain.net 这两个网站都有一个链接到 www.domain.co.uk/iframe.html

I've been looking around but can't find a set answer or solution for this. I have 2 websites, www2.domain.net and www3.domain.net both these websites have an iFrame on them that links to www.domain.co.uk/iframe.html

的iFrame我想要的do是检查父URL,即 - 是访问 www2.domain.net www3.domain.net 的用户。所以我可以向访问 www3.domain.net 的人显示具体信息。

What I want to do is check the parent URL, i.e - is the user accessing www2.domain.net or www3.domain.net so I can show specific information to just people accessing www3.domain.net.

这可能吗?

谢谢,
Andrew

推荐答案

非常简单,并且要求您可以控制涉及的所有三个页面。

It's possible, but not very straightforward and requires that you have control over all three pages involved.

如果您控制所有三个网站,那么就会出现跨窗口消息, window.postMessage() 。然而,它是DOM的一个(相对)新增加,并且在IE 7,IE 6,Safari 4,Firefox 2和Opera 9等旧版浏览器中不受支持。

If you do control all three sites there's cross window messaging, courtesy of window.postMessage(). However, it's a (relatively) new addition to the DOM and unsupported in older browsers like IE 7, IE 6, Safari 4, Firefox 2 and Opera 9.

// Parent:
iframeWin.postMessage("Hello, from "+window.location.host);


// Iframe: 
window.onmessage = function (evt) { alert(evt.origin); }

另一个选项是通过URL传递域名,然后在另一端解析它:

Another option is to pass the domain via the URL and then parse this at the other end:

<iframe src="www.domain.co.uk/iframe.html?www2.domain.net" />

以及所需的JavaScript(从iframe中的页面调用):

and the required JavaScript (called from the page inside the iframe):

alert(window.location.search);
//-> "?www2.domain.net"

这篇关于访问IFrame的父URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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