Javascript-如何使用哈希获取文档引荐来源网址 [英] Javascript - How to get documents referrer with hash

查看:112
本文介绍了Javascript-如何使用哈希获取文档引荐来源网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否可能.我试图在iframe中访问文档的引荐来源网址,但它始终无视hash(#)值.

I wonder if this is possible. I was trying to access the referrer of the document inside an iframe but it always disregard the hash(#) value.

根页(index.html)-(http://example.com/test#hashvalue)

Root Page(index.html) - (http://example.com/test#hashvalue)

在根页面内,有一个iframe获取引荐来源网址.它具有这段代码

Inside the Root Page, there is an iframe that gets the referrer. It has this piece of code

var referrer = document.referrer;

这将返回不包含哈希值的http://example.com/test.有没有一种方法可以获取完整的url(带有散列)作为引荐来源网址.

This returns http://example.com/test without the hash value. Is there a way to get the full url(with hash) as a referrer.

注意:我无权访问根"页面.如果可以,我可以使用postMessage但不能. iframe只是嵌入的(我们称其为根页面的小部件).

Note: that I dont have access to Root page. If I do, I can use postMessage but I don't. The iframe was just embedded(Lets call it as a widget of the root page).

推荐答案

任何包含#字符的URL都是片段URL. #右侧(您要访问的部分)是片段标识符,即页面内的位置.
浏览器没有实现document.referrer来传递此标识符,这与传递给网页的HTTP_REFERRER标头是一致的.

如果两个iframe都来自同一个域,即您可以从子域访问parent.location,则可以使用 parent.location.hash 获取哈希部分. 否则运气不好

Any url that contains the # character is a fragment url. The portion on the right of the # (that you want to access) is a fragment identifier i.e. a location within the page.
The browsers do not implement document.referrer to pass this identifier, this is in line with HTTP_REFERRER header passed to web pages.

If both of your iframes are from same domain i.e you are able to access parent.location from the child, then you can get the hash portion using parent.location.hash. Hard luck otherwise

这篇关于Javascript-如何使用哈希获取文档引荐来源网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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