如何使用 Javascript 从 iframe 实现跨域 URL 访问? [英] How do I implement Cross Domain URL Access from an Iframe using Javascript?

查看:10
本文介绍了如何使用 Javascript 从 iframe 实现跨域 URL 访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从另一个域中的 Iframe 访问父域 URL.

I need to access the Parent Domain URL from my Iframe which is in another domain.

例如,example.com"是我的网站,其中包含来自另一个父域的 Iframe,例如google.com".在这里,我需要从我的 example.com 访问父域 URL.也就是说,我需要在我的example.com"域中获取 URLgoogle.com".此外,父域不能硬编码.

For example, "example.com" is my website which has an Iframe from another parent domain, such as "google.com". Here I need to access the parent domain URL from my example.com. That is, I need to get the URL "google.com" in my "example.com" domain. Moreover, the Parent domain cannot be hard coded.

我尝试使用以下代码:

window.parent.location.href()

但这会导致拒绝访问错误.我如何正确实施以实现这一目标?

but this results in Access denied error. How do I implement this properly in order to achieve this?

推荐答案

您可以尝试检查referer,如果您是iframe,它应该是父站点

You can try and check for the referer, which should be the parent site if you're an iframe

你可以这样做:

var href = document.referrer;

这篇关于如何使用 Javascript 从 iframe 实现跨域 URL 访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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