2016-01-19 51 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中,無論它們是否跨域。