2016-01-19 52 views
0

其他iframe中有一个iframeiframe。我想从第二个iframe获得浏览器的URL (例如:http://test从两级深度iframe中获取浏览器网址

//browser URL: http://test <-- I need this URL 

<iframe src="http://level1"> 
    <iframe src="http://level2"> 
     <!-- some content --> 
    </iframe> 
</iframe> 

我试图document.referrer但它给后面的第一个iframe的网址:

<iframe src="http://level1"> 
    <iframe src="http://level2"> 
     console.log(document.referrer); //It gives back http://level1 
    </iframe> 
</iframe> 

UPDATE:

top.location.href不起作用,因为有不同的域。不同的浏览器,第一个和第二个iframe域。 (我们的网站 - 广告服务器 - 第三方网站)

+0

如果所有页面共享相同的域名,您可以在任何'iframe'中获得最顶层窗口的URL,如下所示:'top.location.href'。 – Teemu

+0

有两个不同的域。我收到错误消息:'Uncaught SecurityError:阻止了一个来自“http://www.firsturl.com”的框架访问源于“http://secondurl.com”的框架。协议,域和端口必须匹配。“ – kotapeter

+2

嗯,猜猜你运气不好。同源政策不会允许你这样做。 – epascarello

回答

2

至于说,这是一般不可能从跨网域的iframe,但在Chrome的情况下,你可以使用window.location.ancestorOrigins,会给你的“顶级网址“和所有域的列表,如果你在几个iframe中,无论它们是否跨域。