简体   繁体   English

如何获取在另一个来源域的iframe中嵌入另一个页面的网页的网址

[英]How to get the url of a webpage that is embedding another page in an iframe on a different origin domain

Webpage A is embedded in an iframe inside of webpage B. A and B are on two different domains and therefore the same origin policy prevents A from accessing properties of B like so; 网页A嵌入在网页B的iframe中。A和B在两个不同的域中,因此相同的原始策略会阻止A像这样访问B的属性;

location = window.top.location.href // emits a "Permission denied" error

Is there any other way for A to get B's url? A还有其他方法获取B的网址吗?

If you have control over both domains, you can try a cross-domain scripting library like EasyXDM , which wrap cross-browser quirks and provide an easy-to-use API for communicating in client script between different domains using the best available mechanism for that browser (eg postMessage if available, other mechanisms if not). 如果您可以控制两个域,则可以尝试使用EasyXDM之类的跨域脚本库,该库包装了跨浏览器的怪癖,并提供了一种易于使用的API,可以使用针对该域的最佳可用机制在客户端脚本中进行通信。浏览器(例如postMessage(如果可用),其他机制(如果没有))。

Caveat: you need to have control over both domains in order to make it work (where "control" means you can place static files on both of them). 注意:您需要同时控制两个域才能使其正常工作(“控制”意味着您可以在两个域上都放置静态文件)。 But you don't need any server-side code changes. 但是您不需要任何服务器端代码更改。

In your case, you'd add javascript into one or both of your pages to look at the location.href, and you'd use the library to call from script in one page into script in the other. 在您的情况下,您可以将javascript添加到一个或两个页面中,以查看location.href,然后使用该库从一个页面中的脚本调用另一个页面中的脚本。

Another Caveat: there are security implications here-- make sure you trust the other domain's script! 另一个警告:这里涉及安全性-确保您信任另一个域的脚本!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM