Html 将 iframe 链接加载到父窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15712880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:00:45  来源:igfitidea点击:

Load iframe links into parent window?

htmliframe

提问by user2203703

I have this iframe code:

我有这个 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>

What i'm trying to do is..when i click any link in iframe page..then i will go to it and the page will load and i will getout from the current page to the iframe link as a new page.

我想要做的是..当我点击 iframe 页面中的任何链接时..然后我会去它,页面将加载,我将从当前页面到 iframe 链接作为一个新页面。

Something like target="_self"- not like the normal iframe which will go to this link without any changes in browser itself.

类似的东西target="_self"- 不像普通的 iframe,它会在浏览器本身没有任何变化的情况下转到此链接。

Example : iframe src : http://www.w3schools.com

示例:iframe src:http: //www.w3schools.com

When i click "Learn HTML" inside the iframe page..then i will go to http://www.w3schools.com/html/default.aspand my browser URL will change to it too.

当我在 iframe 页面中单击“Learn HTML”时..然后我将转到http://www.w3schools.com/html/default.asp并且我的浏览器 URL 也会更改为它。

So i just clicked a link inside the iframe and i go to it.

所以我只是点击了 iframe 中的一个链接,然后我就去了。

Any ideas?

有任何想法吗?

回答by dsgriffin

Yes, you can use target="_parent"to achieve this.

是的,您可以使用target="_parent"来实现这一点。

"target="_parent" opens the linked document in the parent frame."

"target="_parent" 在父框架中打开链接的文档。"

Example:

例子:

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


Edit:

编辑:

If that's not working, you can try _top:

如果这不起作用,您可以尝试_top

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

Or base target:

或者base target

<base target="_parent" />

Or window.top.locationin JavaScript:

或者window.top.location在 JavaScript 中:

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

回答by Adam Plocher

What you're looking for is <a href="..." target="_parent">

你要找的是 <a href="..." target="_parent">

_parentwill cause it to go to the parent frame

_parent将导致它转到父框架

_topwill cause it to go to the top-most level.

_top将导致它进入最顶层。

Either _parentor _topshould work for your target.

要么_parent_top应该为您的目标工作。

回答by Ismael Serrano

You can use javascript to add the target to the links dynamically, something like this.

您可以使用 javascript 将目标动态添加到链接中,就像这样。

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

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

And then add onload="onLoadIF(this)" to the iframe.

然后将 onload="onLoadIF(this)" 添加到 iframe。

回答by Skatox

Yes, just add target="_parent"on the link and it load on main page.

是的,只需添加target="_parent"链接并加载到主页上。

回答by sandeep kumar

If site opens in iframe then redirect to main site

如果站点在 iframe 中打开,则重定向到主站点

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