首页 文章

如何强制在父窗口中打开iframe的链接

提问于
浏览
316

我需要在同一个父页面中打开链接,而不是在新页面中打开它 .

注意: iframe 和父页面是 the same domain .

13 回答

  • 13

    我发现最好的解决方案是使用 base 标签 . 将以下内容添加到iframe页面的头部:

    <base target="_parent">
    

    这将加载父窗口中页面上的所有链接 . 如果您希望在新窗口中加载链接,请使用:

    <base target="_blank">
    

    在所有浏览器中,此标记为fully supported .

  • 8

    使用target-attribute:

    <a target="_parent" href="http://url.org">link</a>
    
  • 7

    使用JavaScript:

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

    You can use any options

    in case of only parent page:

    如果你想打开 all link 到父页面或父iframe,那么你在iframe的head部分使用以下代码:

    <base target="_parent" />

    OR

    如果要打开 specific link 进入父页面或父iframe,则使用以下方式:

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

    OR

    in case of nested iframe:

    如果要在浏览器窗口中打开 all link (在浏览器URL中重定向),那么在iframe的head部分中使用以下代码:

    <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

    如果你想在浏览器窗口中打开 specific link (在浏览器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的HTML元素,它允许您:

    为页面上的所有链接指定默认URL和默认目标:

    <base target="_blank" />
    

    通过指定 _blank ,您可以确保iframe中的所有链接都在外面打开 .

  • 3

    如上所述,您可以使用目标属性,但在技术上已弃用XHTML . 这让你使用javascript,通常像 parent.window.location .

  • 4

    anchor tag 内尝试 target="_parent" attribute .

  • 562

    The most versatile and most cross-browser solution is to avoid use of the "base" tag, and instead use the target attribute of the "a" tags:

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

    <base> 标签的功能较少,浏览器在文档中的位置要求不一致,需要进行更多的跨浏览器测试 . 根据您的项目和情况,实现 <base> 标签的理想跨浏览器放置可能很困难甚至完全不可行 .

    使用 <a> 标记的 target="_parent" 属性执行此操作不仅更易于浏览,而且还允许您区分要在iframe中打开的链接以及要在父级中打开的链接 .

  • 1

    <a target="parent"> 将在新标签/窗口中打开链接... <a target="_parent"> 将打开父/当前窗口中的链接,而无需打开新标签/窗口 . Don't_forget_that_underscore!

  • 22

    如果您在网页中使用iframe,则在通过iframe中的HTML超链接(锚标记)更改整个页面时可能会遇到问题 . 有两种解决方案可以缓解这个问题 .

    解决方案1.您可以使用锚标记的target属性,如以下示例所示 .

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

    解决方案2.您还可以使用JavaScript从iframe在父窗口中打开新页面 .

    <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

  • 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>
    
  • 133

    试试 target="_top"

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

相关问题