2009-06-24 90 views

回答

517

我發現最好的解決方案是使用基地標籤。將以下內容添加到iframe頁面的頭部:

<base target="_parent"> 

這將加載父窗口中頁面上的所有鏈接。如果你希望你的鏈接,加載在一個新的窗口,使用:

<base target="_blank"> 

此標記在所有瀏覽器fully supported

123

使用目標屬性:

<a target="_parent" href="http://url.org">link</a> 
+0

我試試這個,是我在新窗口中 – 2009-06-24 11:52:17

+2

+1開放,基本目標=「_父」的作品,但目標=「_父」,決不放過我! – 2012-03-14 05:18:56

+0

它適用於SVG! – Rustam 2012-06-22 11:28:31

6

嘗試target="_parent"屬性anchor tag內。

9

如上所述,您可以使用目標屬性,但它在技術上已在XHTML中棄用。這留下了您使用JavaScript,通常像parent.window.location

7

有一個DOM對象調用base它允許你:「指定頁面上的所有鏈接默認URL和默認的目標:」通過指定「_blank」

<base target="_blank" /> 

你要確保裏面的所有鏈接iframe將在外部打開。

30

使用JavaScript:

window.parent.location.href= "http://www.google.com"; 
3

<a target="parent">在新標籤/窗口將打開鏈接... <a target="_parent">將打開在父/當前窗口的鏈接,而無需打開新的標籤頁/窗口。 Don't_forget_that_underscore!

4

最通用的和最跨瀏覽器的解決方案是避免使用「基地」標籤的,而是用「a」標記的目標屬性:

<a target="_parent" href="http://www.stackoverflow.com">Stack Overflow</a> 

<base>標籤是功能較少,瀏覽器在文檔中的位置要求不一致,需要更多的跨瀏覽器測試。根據您的項目和情況,實現<base>標籤的理想跨瀏覽器放置可能很困難,甚至完全不可行。

使用<a>標記的target="_parent"屬性執行此操作不僅更適合瀏覽器,還允許您區分要在iframe中打開的那些鏈接以及要在父級打開的鏈接。

1

如果您在網頁中使用iframe,則可能會在通過iframe中的HTML超鏈接(錨標記)更改整個頁面時遇到問題。有兩種解決方案可以緩解這個問題。

解決方案1.您可以使用以下示例中給出的錨標記的目標屬性。

<a target="_parent" href="http://www.kriblog.com">link</a> 

解決方案2.您也可以在iframe和JavaScript的父窗口中打開一個新頁面。

<a href="#" onclick="window.parent.location.href='http://www.kriblog.com';"> 

記住⇒target="_parent"已過時,XHTML,但它仍然是在HTML 5.x中支持

更可以從下面的鏈接 http://www.kriblog.com/html/link-of-iframe-open-in-the-parent-window.html

16

讀你可以使用任何選項

只有父頁面的情況下:

,如果你想打開所有鏈接到父頁面或父級iframe中,然後在iframe的頭部分中使用以下代碼:

<base target="_parent" />

OR,如果你想開一家特定鏈接到父頁面或父IFRAME

,那麼您可以採取以下方式:

<a target="_parent" href="http://specific.org">specific Link</a> 
<a href="http://normal.org">Normal Link</a> 

OR

萬一嵌套iframe的:

如果想打開所有鏈接到瀏覽器窗口(在瀏覽器的URL重定向),那麼您可以採取以下的iframe的頭部分代碼:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(window).load(function(){ 
     $("a").click(function(){ 
      top.window.location.href=$(this).attr("href"); 
      return true; 
     }) 
    }) 
</script> 

OR

如果要打開特定鏈接進入瀏覽器窗口(在瀏覽器url中重定向),則使用以下方法:

<a href="http://specific.org" target="_top" >specific Link</a> 

<a href="javascript:top.window.location.href='your_link'">specific Link</a> 
<a href="javascript:top.window.location.href='http://specific.org'">specific Link</a> 
<a href="http://normal.org">Normal Link</a> 
0

耶,我發現

<base target="_parent" /> 

這個有用的開放在iframe中打開的所有鏈接的iframe。

而且

$(window).load(function(){ 
    $("a").click(function(){ 
     top.window.location.href=$(this).attr("href"); 
     return true; 
    }) 
}) 

這一點我們可以使用整個網頁或網頁的特定部分。

謝謝大家的幫助。

0
<script type="text/javascript"> // if site open in iframe then redirect to main site 
     $(function(){ 
      if(window.top.location != window.self.location) 
      { 
       top.window.location.href = window.self.location; 
      } 
     }); 
    </script> 
0

嘗試target="_top"

<a href="http://example.com" target="_top"> 
    This link will open in same but parent window of iframe. 
</a> 
相關問題