Html target="_blank" 与 target="_new"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4964130/
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
target="_blank" vs. target="_new"
提问by Alex Grin
What's the difference between <a target="_new">
and <a target="_blank">
and which should I use if I just want to open a link in a new tab/window?
如果我只想在新选项卡/窗口中打开链接,我应该使用<a target="_new">
和<a target="_blank">
和有什么区别?
回答by fresskoma
Use "_blank"
使用“_blank”
According to the HTML5 Spec:
根据HTML5 规范:
A valid browsing context nameis any string with at least one character that does not start with a U+005F LOW LINE character. (Names starting with an underscore are reserved for special keywords.)
A valid browsing context name or keywordis any string that is either a valid browsing context name or that is an ASCII case-insensitive match for one of: _blank, _self, _parent, or _top." - Source
一个有效的浏览上下文名称是至少一个字符的任何字符串,它不与U + 005F低线字符开头。(以下划线开头的名称保留用于特殊关键字。)
一个有效的浏览上下文名称或关键字是一个有效的浏览上下文名称的任何字符串或者是一个一个ASCII不区分大小写的匹配:_blank,_self,_parent,_top或“ -来源
That means that there is no such keyword as _new
in HTML5, and not in HTML4 (and consequently XHTML) either. That means, that there will be no consistent behavior whatsoever if you use this as a value for the target attribute.
这意味着_new
在 HTML5 中没有这样的关键字,在 HTML4(以及 XHTML)中也没有。这意味着,如果您将其用作目标属性的值,则不会有任何一致的行为。
Security recommendation
安全建议
As Daniel and Michael have pointed out in the comments, when using target _blank
pointing to an untrusted website, you should, in addition, set rel="noopener"
. This prevents the opening site to mess with the opener via JavaScript. See this postfor more information.
正如 Daniel 和 Michael 在评论中指出的那样,当使用 target_blank
指向不受信任的网站时,您还应该设置rel="noopener"
. 这可以防止打开站点通过 JavaScript 干扰打开程序。有关更多信息,请参阅此帖子。
回答by Abhi Beckert
Using target="_blank"
will instruct the browser to create a new browser tab or window when the user clicks on the link.
target="_blank"
当用户单击链接时,使用将指示浏览器创建一个新的浏览器选项卡或窗口。
Using target="_new"
is technically invalid according to the specifications, but as far as I know every browser will behave the same way:
target="_new"
根据规范,使用在技术上是无效的,但据我所知,每个浏览器的行为方式都是一样的:
- it will search for a tab or window with the context name "_new"
- if a "_new" tab/window is found, then the URL is loaded into it
- if it's not found, a new tab/window is created with the context name "_new", and the URL loaded into it
- 它将搜索上下文名称为“_new”的选项卡或窗口
- 如果找到“_new”选项卡/窗口,则将 URL 加载到其中
- 如果未找到,则会创建一个新的选项卡/窗口,其上下文名称为“_new”,并将 URL 加载到其中
Note target="_new"
will behave exactly the same as target="new"
, and the latter is valid HTML while the former is invalid HTML.
Note 的target="_new"
行为与 完全相同target="new"
,后者是有效的 HTML,而前者是无效的 HTML。
Adding some confusion to this, in HTML4 the target
attribute was deprecated. In HTML5 this decision was reversed, and it is an official part of the spec once again. All browsers support target
no matter what version of HTML you are using, but some validators will flag the use as deprecated if your doctype is HTML4.
对此添加一些混淆,在 HTML4 中该target
属性已被弃用。在 HTML5 中,这个决定被颠倒了,它再次成为规范的官方部分。target
无论您使用什么版本的 HTML,所有浏览器都支持,但如果您的 doctype 是 HTML4,一些验证器会将使用标记为已弃用。
回答by abe312
TL;DR
USE _blank
TL;DR
使用 _blank
The target attribute specifies where to open the linked document.
target 属性指定打开链接文档的位置。
USAGE: target="xyz" [don't forget double quotes]
_blank Opens the linked document in a new window or tab
_self Opens the linked document in the same frame as it was clicked (this is default)
_parent Opens the linked document in the parent frame
_top Opens the linked document in the full body of the window
framename Opens the linked document in a named frame
SINCE "_new" is not any of these IT WILL COME UNDER "framename" so if a user re-clicks on that hyperlink it will not open a new tab instead update the existing tab. Whereas in _blank if user clicks twice then 2 new tabs open.
由于“_new”不是其中的任何一个,因此它会出现在“framename”下,因此如果用户重新单击该超链接,它将不会打开新选项卡,而是更新现有选项卡。而在 _blank 中,如果用户单击两次,则会打开 2 个新选项卡。
回答by Michal Frystacky
I know this is an old question and the correct answer, use _blank
, has been mentioned several times, but using <a target="somesite.com" target="_blank">Link</a>
is a security risk.
我知道这是一个老问题,正确答案 use_blank
已被多次提及,但使用<a target="somesite.com" target="_blank">Link</a>
存在安全风险。
It is recommended(performance benefits) to use:
<a href="somesite.com" target="_blank" rel="noopener noreferrer">Link</a>
回答by corroded
This may have been asked before but:
之前可能已经问过这个问题,但是:
"every link that specifies target="_new" looks for and finds that window by name, and opens in it.
“指定 target="_new" 的每个链接都会按名称查找并找到该窗口,并在其中打开。
If you use target="_blank," a brand new window will be created each time, on top of the current window."
如果您使用 target="_blank",则每次都会在当前窗口的顶部创建一个全新的窗口。
from here: http://thedesignspace.net/MT2archives/000316.html
回答by Abudayah
target="_blank"
opens a new tab in most browsers.
target="_blank"
在大多数浏览器中打开一个新标签。
回答by the Hampster
it's my understanding that target = whatever
will look for a frame/window with that name. If not found, it will open up a new window with that name. If whatever == "_new"
, it will appear just as if you used _blank
except.....
我的理解是target = whatever
将查找具有该名称的框架/窗口。如果未找到,它将打开一个具有该名称的新窗口。如果whatever == "_new"
,它看起来就像你使用了_blank
except.....
Using one of the reserved target names will bypass the "looking" phase. So, target = "_blank"
on a dozen links will open up a dozen blank windows, but target = whatever
on a dozen links will only open up one window. target = "_new"
on a dozen links may give inconstant behavior. I haven't tried it on several browsers, but should only open up one window.
使用保留的目标名称之一将绕过“查找”阶段。所以,target = "_blank"
在十几个链接上会打开十几个空白窗口,而target = whatever
在十几个链接上只会打开一个窗口。target = "_new"
在十几个链接上可能会出现不稳定的行为。我还没有在几个浏览器上尝试过,但应该只打开一个窗口。
At least this is how I interpret the rules.
至少我是这样解释规则的。
回答by cnd
Caution - remember to always include the "quotes" - at least on Chrome, target=_blank
(no quotes) is NOT THE SAME as target="_blank"
(with quotes).
注意 - 请记住始终包含“引号” - 至少在 Chrome 上,target=_blank
(无引号)与target="_blank"
(带引号)不同。
The latter opens each link in a new tab/window. The former (missing quotes) opens the first link you click in one new tab/window, then overwrites that same tab/window with each subsequent link you click (that's named also with the missing quotes).
后者在新选项卡/窗口中打开每个链接。前者(缺少引号)打开您在一个新选项卡/窗口中单击的第一个链接,然后用您单击的每个后续链接(也用缺少的引号命名)覆盖同一个选项卡/窗口。
回答by James Drinkard
- _blank as a target value will spawn a new window every time,
- _new will only spawn one new window.
- _blank 作为目标值每次都会产生一个新窗口,
- _new 只会产生一个新窗口。
Also, every link clicked with a target value of _new will replace the page loaded in the previously spawned window.
此外,目标值为 _new 的每个链接都将替换之前生成的窗口中加载的页面。
You can click here When to use _blank or _newto try it out for yourself.
您可以单击此处何时使用 _blank 或 _new亲自尝试一下。
回答by Daniel
In order to open a link in a new tab/window you'll use <a target="_blank">
.
为了在新选项卡/窗口中打开链接,您将使用<a target="_blank">
.
value _blank
= targeted browsing context: a new one: tab or window depending on your browsing settings
值_blank
= 目标浏览上下文:一个新的:选项卡或窗口取决于您的浏览设置
value _new
= not valid; no such value in HTML5 for target attribute on a element
值_new
= 无效;元素上的目标属性在 HTML5 中没有这样的值
target attribute with all its values on a element: video demo
target 属性及其在元素上的所有值:视频演示