javascript 如何获取 iframe 父页面的引用源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23766046/
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
How to get referrer source of iframe parent page?
提问by Giancarlo Massaro
We are taking our iframe entry form code and placing it on a website page. We want to be able to see where someone came from if they land on the page and entered their information into the form.
我们正在获取我们的 iframe 输入表单代码并将其放置在网站页面上。如果某人登陆页面并将其信息输入表单,我们希望能够看到他们来自哪里。
For example: our iframe is embedded on a page. The URL of that page gets posted to Facebook. Someone clicks the link, lands on the page, and enters their information into our iframe entry form. We then want to be able to say, 1 referral came from Facebook.
例如:我们的 iframe 嵌入在页面上。该页面的 URL 被发布到 Facebook。有人点击链接,登陆页面,然后将他们的信息输入到我们的 iframe 输入表单中。然后我们希望能够说,1 个推荐来自 Facebook。
We tried using $_SERVER['HTTP_REFERER'], but this just returns the parent page URL of where the iframe is embedded, which we don't want. We want the actual URL of the referral to the parent page (in the example above, it would be Facebook). Is there anyway to grab this information?
我们尝试使用 $_SERVER['HTTP_REFERER'],但这只是返回嵌入 iframe 的父页面 URL,这是我们不想要的。我们想要引用父页面的实际 URL(在上面的示例中,它将是 Facebook)。有没有办法获取这些信息?
回答by earl3s
Unfortunately cross-domain security is going to block this information from you since your iframe is not on the same domain as the parent page that is embedding it. You can check the referrer in your iframe but it will only give you the name of the page that is embedding it.
不幸的是,跨域安全性将阻止您获取此信息,因为您的 iframe 与嵌入它的父页面不在同一个域中。您可以检查 iframe 中的引荐来源网址,但它只会为您提供嵌入它的页面的名称。
If you were on the same domain you would have access to the document.referrer
in javascript and could retrieve that through the iframe.
如果您在同一个域中,您将可以访问document.referrer
in javascript 并可以通过 iframe 检索它。
I do have an idea for a solution though.
不过,我确实有一个解决方案的想法。
If you used a script to embed the iframe on the parent page you could do something like this.
如果您使用脚本将 iframe 嵌入到父页面上,您可以执行以下操作。
function createIFrame() {
var ref = document.referrer;
ifrm = document.createElement("iframe");
ifrm.setAttribute("src", "http://www.nba.com/?referrer="+ref);
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
document.body.appendChild(ifrm);
}
createIFrame();
This would allow you to then in your iframe read the query string referrer
from your url and send that information to your server. It would require you to package up a little bit of javascript with your widget, but it may be the only solution to you.
这将允许您在 iframe 中referrer
从您的 url读取查询字符串并将该信息发送到您的服务器。这将需要您将一些 javascript 与您的小部件打包在一起,但这可能是您唯一的解决方案。
JSFiddle - http://jsfiddle.net/7QbPN/
JSFiddle - http://jsfiddle.net/7QbPN/