Html 如何强制在父窗口中打开来自 iframe 的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1037839/
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
How to force link from iframe to be opened in the parent window
提问by Haim Evgi
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.
注意:iframe
和父页面是同一个域。
回答by Chris Vasselli
I found the best solution was to use the basetag. Add the following to the head of the page in the iframe:
我发现最好的解决方案是使用基本标签。将以下内容添加到 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 supportedin all browsers.
所有浏览器都完全支持此标签。
回答by nnevala
Use target-attribute:
使用目标属性:
<a target="_parent" href="http://url.org">link</a>
回答by yenssen
With JavaScript:
使用 JavaScript:
window.parent.location.href= "http://www.google.com";
回答by Ahosan Karim Asik
You can use any options
您可以使用任何选项
in case of only parent page:
如果只有父页面:
if you want to open all linkinto parent page or parent iframe, then you use following code in head section of iframe:
如果要将所有链接打开到父页面或父 iframe 中,则在 iframe 的 head 部分使用以下代码:
<base target="_parent" />
<base target="_parent" />
OR
或者
if you want to open a specific linkinto parent page or parent iframe, then you use following way:
如果要打开父页面或父 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:
在嵌套 iframe 的情况下:
If want to open all linkinto browser window (redirect in browser url), then you use following code in head section of iframe:
如果要将所有链接打开到浏览器窗口(在浏览器 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
或者
if you want to open a specific linkinto browser window (redirect in browser url), then you use following way:
如果要在浏览器窗口中打开特定链接(在浏览器 url 中重定向),则使用以下方式:
<a href="http://specific.org" target="_top" >specific Link</a>
or
或者
<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>
回答by vsync
There's a HTML element called base
which allows you to:
有一个名为的 HTML 元素base
,它允许您:
Specify a default URL and a default target for all links on a page:
为页面上的所有链接指定默认 URL 和默认目标:
<base target="_blank" />
By specifying _blank
you make sure all links inside the iframewill be opened outside.
通过指定,_blank
您可以确保iframe内的所有链接都将在外部打开。
回答by Gee
Try target="_parent"
attributeinside the anchor tag
.
尝试target="_parent"
属性里面anchor tag
。
回答by Ryan McGrath
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
.
如前所述,您可以使用目标属性,但它在 XHTML 中在技术上已被弃用。这让您只能使用 javascript,通常类似于parent.window.location
.
回答by user1788142
If you are using iframe in your webpage you might encounter a problem while changing the whole page through a HTML hyperlink (anchor tag) from the iframe. There are two solutions to mitigate this problem.
如果您在网页中使用 iframe,则在通过 iframe 中的 HTML 超链接(锚标记)更改整个页面时可能会遇到问题。有两种解决方案可以缓解这个问题。
Solution 1. You can use target attribute of anchor tag as given in the following example.
解决方案 1. 您可以使用以下示例中给出的锚标记的目标属性。
<a target="_parent" href="http://www.kriblog.com">link</a>
Solution 2. You can also open a new page in parent window from iframe with JavaScript.
解决方案 2. 您也可以使用 JavaScript 从 iframe 在父窗口中打开一个新页面。
<a href="#" onclick="window.parent.location.href='http://www.kriblog.com';">
Remember ? target="_parent"
has been deprecated in XHTML, but it is still supported in HTML 5.x.
记住 ?target="_parent"
在 XHTML 中已被弃用,但在 HTML 5.x 中仍受支持。
More can be read from following link http://www.kriblog.com/html/link-of-iframe-open-in-the-parent-window.html
可以从以下链接阅读更多内容 http://www.kriblog.com/html/link-of-iframe-open-in-the-parent-window.html
回答by Adelmar
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:
最通用和最跨浏览器的解决方案是避免使用“base”标签,而是使用“a”标签的目标属性:
<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.
该<base>
标签是通用性较差,而浏览器是在他们对文档中的位置要求不符,需要更多的跨浏览器测试。根据您的项目和情况,实现理想的<base>
标签跨浏览器放置可能很困难,甚至完全不可行。
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.
用标签的target="_parent"
属性这样做<a>
不仅对浏览器更友好,而且可以让你区分那些你想在 iframe 中打开的链接,和那些你想在父级中打开的链接。
回答by krisjd
<a target="parent">
will open links in a new tab/window ... <a target="_parent">
will open links in the parent/current window, without opening new tabs/windows. Don't_forget_that_underscore!
<a target="parent">
将在新标签/窗口中<a target="_parent">
打开链接...将在父/当前窗口中打开链接,而不打开新标签/窗口。不要_忘记_那个_下划线!