javascript document.referrer 和 window.parent.location.href 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16413903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:33:43  来源:igfitidea点击:

Difference between document.referrer and window.parent.location.href

javascriptiframe

提问by k102

Here's the situation: there is a site, and it belongs to the client, so it's not on my domain, lets say client.com.

情况是这样的:有一个站点,它属于客户,所以它不在我的域中,比如说client.com.

On this site there is an iframe, the source of this iframeis a simple js code, which loads another js (client.js) - this code is on my domain.

在这个站点上有一个iframe,它的来源iframe是一个简单的 js 代码,它加载另一个 js ( client.js) - 这段代码在我的域中。

What I need to do is to get the exact url of the page where the iframe is. So now I'm trying to fugure out the difference between document.referrerand window.parent.location.hrefwith no luck.

我需要做的是获取 iframe 所在页面的确切 url。所以现在我试图找出document.referrerwindow.parent.location.href没有运气之间的区别。

Both give me exactly what I need, but I can't realize what is more reliable? Is there a situation, where one will work and another won't?

两者都给了我我需要的东西,但我无法意识到什么更可靠?有没有一种情况,一个可以工作,另一个不工作?

回答by deceze

document.referrergives you the URI of the page that linked to the current page. This is a value that's available for all pages, not just frames.

document.referrer为您提供链接到当前页面的页面的 URI。这是一个适用于所有页面的值,而不仅仅是框架。

window.parentgives you the parent frame, and its locationis its URI.

window.parent给你父框架,它location是它的 URI。

If you want to find the URI of the parent frame, then use window.parent.location.

如果要查找父框架的 URI,请使用window.parent.location.

回答by SWilk

The main difference is that the document.referrerwill point to the page which linked to the current page inside the iframe. If your iframe content contain links, which allows to navigate through a few pages, then only the first page loaded inside the iframe will have parent frame URI as document.referrer. Each page loaded by clicking link inside the iframe will have the uri of the page containing link in the document.referrer.

主要区别在于document.referrerwill 指向链接到 iframe 内当前页面的页面。如果您的 iframe 内容包含允许浏览几个页面的链接,则只有在 iframe 内加载的第一个页面才会将父框架 URI 设为document.referrer. 通过单击 iframe 内的链接加载的每个页面都将在document.referrer.

At the same time window.parent.locationwill always contain the URI of the page in parent window, but it will be accessible only if the site origin is the same. Read about relaxing site origin policyto see what should be done on both your and your client sites, so you can access the data.

同时window.parent.location将始终包含父窗口中页面的 URI,但只有在站点来源相同时才能访问它。阅读有关放宽站点原始策略的信息,了解应该在您的站点和您的客户端站点上执行哪些操作,以便您可以访问数据。

That being said, I would rather give your client something like a service key or token, which will authorize his site to use your iframed app, and which will authenticate the caller as your client, so you can know that the call is from his site.

话虽如此,我宁愿给您的客户提供诸如服务密钥或令牌之类的东西,这将授权他的站点使用您的 iframe 应用程序,并将调用者身份验证为您的客户,因此您可以知道呼叫来自他的站点.