2013-03-29 79 views
24

我有這樣的iframe代碼:加載iframe鏈接到父窗口?

<iframe src="http://www.example.com" border="0" framespacing="0" marginheight="0" marginwidth="0" vspace="0" hspace="0" scrolling="yes" width="100%" frameborder="0" height="100%"></iframe> 

我想要做的is..when我點擊iframe中的任何鏈接page..then我會去,並在頁面將加載,我會getout從當前頁面到iframe鏈接作爲新頁面。

類似於target="_self" - 不像正常的iframe,它會在沒有任何瀏覽器本身變化的情況下進入該鏈接。

例子: IFRAME SRC:http://www.w3schools.com

當我點擊 「學習HTML」 的iframe中page..then我會去http://www.w3schools.com/html/default.asp 和我的瀏覽器的URL會改變它了。

所以我只是點擊了iframe中的鏈接,我去了它。

任何想法?

回答

35

是的,您可以使用target="_parent"來實現此目的。

「target =」_ parent「在父框架中打開鏈接的文檔。」

例子:

<a target="_parent" href="http://example.org">Click me!</a> 

編輯:

如果不工作,你可以嘗試_top

<a target="_top" href="http://example.org">Click me!</a> 

或者base target

<base target="_parent" /> 

或者window.top.location在JavaScript:

window.top.location = "http://example.com"; 
+0

Q具有了updated..thank你。 – user2203703

+0

@ user2203703我不明白,這回答了這個問題。您在iframe中的鏈接上使用目標父級.. – lifetimes

+0

我想離開我的頁面,其中有iframe和goout到我在iframe中單擊的鏈接 – user2203703

13

你在找什麼是<a href="..." target="_parent">

_parent將導致它去父框架

_top將導致它達到最高級別。

_parent_top應該適合您的目標。

+1

也許我對此問題並不清楚。 iframe中的頁面...是否可以控制該頁面中的HTML,還是第三方網站?我更新了我的答案,以包含target = _top,但當然這是假設您可以控制幀的內容。 –

5

是的,只需在鏈接上添加target="_parent"並加載到主頁面上即可。

+0

Q已更新..謝謝。 – user2203703

6

您可以使用javascript將目標動態添加到鏈接,如下所示。

function onLoadIF(frame) 
{ 
    var elements = frame.getElementsByTagName('a'), 
     len = elements.length; 

    while(len--) { 
     elements[len].target = "_parent"; 
    } 
} 

然後將onload =「onLoadIF(this)」添加到iframe中。

1

如果網站在iframe中打開然後重定向到主站點

<script> 
    if (window.top.location != window.self.location) { 
     top.window.location.href = window.self.location; 
    } 
</script> 
+0

這是一個黑客,它甚至不回答這個問題。 –