chr*_*war 169 javascript iframe cross-domain
好的,我有一个页面,在这个页面上我有一个iframe.我需要做的是在iframe页面上,找出主页面的URL是什么.
我已经搜索过,我知道如果我的iframe页面位于不同的域上,这是不可能的,因为这是跨站点脚本.但是我读过的每个地方都说如果iframe页面和父页面在同一个域中,那么如果我这样做,它应该可以工作:
parent.document.location
Run Code Online (Sandbox Code Playgroud)
parent.window.document.location
Run Code Online (Sandbox Code Playgroud)
parent.window.location
Run Code Online (Sandbox Code Playgroud)
parent.document.location.href
Run Code Online (Sandbox Code Playgroud)
...或其他类似的组合,因为似乎有多种方法来获得相同的信息.
不管怎么说,这就是问题所在.我的iframe与主页面位于同一个域中,但它不在同一个SUB域中.所以我举个例子
http:// www.mysite.com/pageA.html
然后我的iframe网址是
http:// qa-www.mysite.com/pageB.html
当我尝试从pageB.html(iframe页面)获取URL时,我不断收到相同的访问被拒绝错误.所以看起来甚至子域都算作跨站点脚本,这是正确的,还是我做错了什么?
小智 330
是的,如果iframe和主页不在同一(子)域中,则不允许访问父页面的URL.但是,如果您只需要主页面的URL(即浏览器URL),您可以尝试这样做:
var url = (window.location != window.parent.location)
? document.referrer
: document.location.href;
Run Code Online (Sandbox Code Playgroud)
注意:
window.parent.location被允许; 它避免了OP中的安全性错误,这是由访问href属性引起的:window.parent.location.href导致"阻止带有原始的框架......"
document.referrer指的是"链接到此页面的页面的URI".如果某些其他来源确定了位置,则可能不会返回包含文档iframe,例如:
document.referrer将是域3,而不是包含域1document.location指"位置对象,其中包含有关文档URL的信息"; 可能是当前文档,即iframe当前打开.当window.location === window.parent.location,那么iframe的href是同为含母公司href.
小智 48
我刚刚发现这个问题的解决方法非常简单,但我还没有找到任何提及它的讨论.它确实需要控制父帧.
在你的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>
Run Code Online (Sandbox Code Playgroud)
然后,找到一个javascript url解析函数,解析url字符串以获取你所追求的url变量,在这种情况下它是"url".
我在这里找到了一个很棒的url字符串解析器:http: //www.netlobo.com/url_query_string_javascript.html
Par*_*Par 30
如果你的iframe来自另一个域(跨域),你只需要使用它:
var currentUrl = document.referrer;
Run Code Online (Sandbox Code Playgroud)
而且 - 这里你有主要的网址!
Dan*_*ert 19
你说的没错.使用iframe时,子域仍被视为单独的域.可以使用传递消息postMessage(...),但有意使其他JS API无法访问.
它仍然可以根据上下文获取URL.有关详细信息,请参阅其他答案
小智 18
对于同一域和不同子域的页面,您可以document.domain通过javascript 设置该属性.
父框架和iframe都需要将其document.domain设置为它们之间通用的内容.
也就是说
www.foo.mydomain.com,api.foo.mydomain.com每个人都可以使用foo.mydomain.com或只是mydomain.com兼容(不com,出于安全原因,你不能将它们都设置为......)
另外,请注意document.domain是一条单行道.请考虑按顺序运行以下三个语句:
// assume we're starting at www.foo.mydomain.com
document.domain = "foo.mydomain.com" // works
document.domain = "mydomain.com" // works
document.domain = "foo.mydomain.com" // throws a security exception
Run Code Online (Sandbox Code Playgroud)
现代浏览器也可以使用window.postMessage来交换原点,但它在IE6中不起作用. https://developer.mozilla.org/en/DOM/window.postMessage
J S*_*ott 16
这个问题有很多答案,但就支持或可靠性而言,没有一个答案绝对是最好的。
window.location.ancestorOrigins[0]将获取父 url,但此 api 仅适用于 chromium 浏览器(请参阅支持)。这还支持嵌套 iframe,其中最底部的子级可以访问每个父级 iframe 的 url。document.referrer是最常见的答案,但并不总是可靠
window.parent.location.href。如果父级和子级位于不同的域中,则该 api 会被现代浏览器阻止(请参阅此处),并会抛出错误。如果支持,我更喜欢window.location.ancestorOrigins[0]。document.referrer可以工作,但不太可靠,使其成为我的后备选择。如果您确实使用文档引荐来源网址,请尝试在导航或重定向之前在第一个框架页面加载时调用它,以获取父地址。
小智 14
以下行将起作用:document.location.ancestorOrigins[0]该行返回祖先域名。
小智 6
Try it:
document.referrer
Run Code Online (Sandbox Code Playgroud)
When you change you are in a iframe your host is "referrer".
| 归档时间: |
|
| 查看次数: |
237495 次 |
| 最近记录: |