好的,我有一个页面在这个页面上有一个iframe。我需要做的是在iframe页面上,找出主页面的URL是什么。
我四处搜索,我知道这是不可能的,如果我的iframe页面是在不同的域,因为这是跨站脚本。但我读过的所有地方都说,如果iframe页面与父页面在同一个域上,它应该工作,例如:
parent.document.location
parent.window.document.location
parent.window.location
parent.document.location.href
…或者其他类似的组合,因为似乎有多种方法可以获得相同的信息。
不管怎样,问题来了。我的iframe与主页在同一个域上,但不是在同一个SUB域上。举个例子
http:// www.mysite.com/pageA.html
然后我的iframe URL是
http:// qa-www.mysite.com/pageB.html
当我试图从pageB.html (iframe页面)抓取URL时,我一直得到相同的访问拒绝错误。因此,即使子域也可以算作跨站脚本,这是正确的,还是我做错了什么?
我刚刚发现了一个解决这个问题的简单方法,但我还没有发现任何地方提到它的讨论。它需要控制父帧。
在你的iFrame中,假设你想要这样的iFrame: src="http://www.example.com/mypage.php"
好吧,而不是HTML来指定iframe,使用javascript来为你的iframe构建HTML,通过javascript“在构建时”获得父url,并将其作为url get参数在你的src目标的查询字符串中发送,如下所示:
<script type="text/javascript">
url = parent.document.URL;
document.write('<iframe src="http://example.com/mydata/page.php?url=' + url + '"></iframe>');
</script>
然后,找到自己的javascript url解析函数,解析url字符串,以获得您所追求的url变量,在这种情况下,它是“url”。
我在这里找到了一个很棒的url字符串解析器:
http://www.netlobo.com/url_query_string_javascript.html
这个问题有很多答案,但在支持或可靠性方面,没有一个肯定是最好的。
选项
window.location.ancestorOrigins[0] will get the parent url, but this api only works in chromium browsers (see support). this also supports nested iframes, where the bottom most child has access to the urls of each parent iframe.
document.referrer is the most common answer but is not always reliable
navigation inside of the iframe would show the last page instead of the parent frame url
redirects only show the most recent referrer to the current page
if the page reloads itself the referer becomes the page itself
http child with an https parent has an empty string for referrer
不管用的东西
window.parent.location.href。如果父节点和子节点在不同的域中,这个api会被现代浏览器阻塞(见这里),并抛出一个错误。
建议
如果支持,我更喜欢window.location.祖宗起源[0]。文档。推荐器可以工作,但不太可靠,使其成为我的备用选项。如果您确实使用文档引用器,请尝试在第一次加载框架页面时调用它,然后再进行导航或重定向,以获取父地址。