How to force link from iframe to be opened in the

2019-01-01 07:40发布

I need to open the link in the same parent page, instead of open it in a new page.

note : The iframe and parent page are the same domain.

13条回答
皆成旧梦
2楼-- · 2019-01-01 08:19

I found the best solution was to use the base tag. Add the following to the head of the page in the iframe:

<base target="_parent">

This will load all links on the page in the parent window. If you want your links to load in a new window, use:

<base target="_blank">

This tag is fully supported in all browsers.

查看更多
孤独总比滥情好
3楼-- · 2019-01-01 08:20

Yah I found

<base target="_parent" />

This useful for open all iframe links open in iframe.

And

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

This we can use for whole page or specific part of page.

Thanks all for your help.

查看更多
梦寄多情
4楼-- · 2019-01-01 08:25

With JavaScript:

window.parent.location.href= "http://www.google.com";
查看更多
浪荡孟婆
5楼-- · 2019-01-01 08:25

As noted, you could use a target attribute, but it was technically deprecated in XHTML. That leaves you with using javascript, usually something like parent.window.location.

查看更多
弹指情弦暗扣
6楼-- · 2019-01-01 08:27

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>

The <base> tag is less versatile and browsers are inconsistent in their requirements for its placement within the document, requiring more cross-browser testing. Depending on your project and situation, it can be difficult or even totally unfeasible to achieve the ideal cross-browser placement of the <base> tag.

Doing this with the target="_parent" attribute of the <a> tag is not only more browser-friendly, but also allows you to distinguish between those links you want to open in the iframe, and those you want to open in the parent.

查看更多
人气声优
7楼-- · 2019-01-01 08:29

Try target="_top"

<a href="http://example.com" target="_top">
   This link will open in same but parent window of iframe.
</a>
查看更多
登录 后发表回答