Javascript 在 HTML5 中使用 target="_blank" 可以吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4198788/
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-23 11:20:55  来源:igfitidea点击:

Is it alright to use target="_blank" in HTML5?

javascripthtmlwindow

提问by Darryl Hein

I recall reading somewhere that in HTML5 it was no longer okay to use target="_blank"in HTML5, but I can't find it now.

我记得在某个地方读到过,在 HTML5 中不再可以target="_blank"在 HTML5 中使用,但我现在找不到了。

Is it alright to continue to use target="_blank"?

可以继续使用target="_blank"吗?

I know it's generally considered a bad idea, but it's by the easiest way to open a new window for something like a PDF, and it also doesn't require you to rely on JavaScript.

我知道这通常被认为是一个坏主意,但这是为 PDF 之类的东西打开新窗口的最简单方法,而且它也不需要您依赖 JavaScript。

采纳答案by mike

It looks like target="_blank"is still alright. It is listed as a browsing context keyword in the latest HTML5 draft.

看起来target="_blank"还是可以的。它在最新的 HTML5 草案中被列为浏览上下文关键字。

回答by Graham

It is ok to use target="_blank"; This was done away with in XHTML because targeting new windows will always bring up the pop-up alert in most browsers. XHTML will always show an error with the target attribute in a validate.

可以使用target="_blank"; 这在 XHTML 中被取消了,因为在大多数浏览器中定位新窗口总是会弹出弹出警报。XHTML 将始终在验证中显示带有目标属性的错误。

HTML 5 brought it back because we still use it. It's our friend and we can't let go.

HTML 5 把它带回来了,因为我们仍在使用它。这是我们的朋友,我们不能放手。

Never let go.

绝不放手。

回答by chharvey

Though the target="_blank"is acceptable in HTML5, I personally try never to use it (even for opening PDFs in a new window).

尽管在 HTML5 中target="_blank"可以接受的,但我个人尝试从不使用它(即使是在新窗口中打开 PDF)。

HTML should define meaning and content. Ask yourself, “would the meaningof the aelement change if the targetattribute were removed?”If not, the code should not go in the HTML. (Actually I'm surprised the W3C kept it… I guess they really just can't let go.)

HTML 应该定义含义和内容。问问自己,“会的意义的的a元素变化,如果target属性被拆除?” 如果不是,则代码不应出现在 HTML 中。(实际上我很惊讶 W3C 保留它......我猜他们真的不能放手。)

Browser behavior, specifically, interactive behavior with the user, should be implemented with client-side scripting languages like JavaScript. Since you want the browser to behave in a particular way, i.e., opening a new window, you should use JS. But as you mentioned, this behavior requires the browser to rely on JS. (Though if your site degrades gracefully, or enhances progressively, or whatever, then it should still be okay. The users with JS disabled won't miss much.)

浏览器行为,特别是与用户的交互行为,应该使用 JavaScript 等客户端脚本语言来实现。由于您希望浏览器以特定方式运行,即打开一个新窗口,您应该使用 JS。但是正如你提到的,这种行为需要浏览器依赖 JS。(尽管如果您的网站优雅地降级,或逐渐增强,或者其他什么,那么它应该仍然可以。禁用 JS 的用户不会错过太多。)

That being said, neither of these is the right answer. Out there somewhereis the opinion that how a link opens should ultimately be decided by the end user.Take this example.

话虽如此,这些都不是正确的答案。一种观点认为链接的打开方式最终应由最终用户决定以这个例子为例。

You're surfing Wikipedia, getting deeper and deeper into a rabbit hole. You come across a link in your reading.

Let's say you want to skim the linked page real quick before coming back. You might open it in a new tab, and then close it when you're done (because hitting the ‘back' button and waiting for page reload takes too long). Or, what if it looks interesting and you want to save it for later? Maybe you should open it in a new background tab instead, and keep reading the current page. Or, maybe you decide you're done reading this page, so you'll just follow the link in the current tab.

The point is, you have your own workflow, and you'd like your browser to behave accordingly. You might get pretty frustrated if it made these kinds of decisions for you.

你正在浏览维基百科,越来越深入一个兔子洞。您在阅读中遇到了一个链接。

假设您想在返回之前快速浏览链接页面。您可以在新选项卡中打开它,然后在完成后关闭它(因为点击“后退”按钮并等待页面重新加载需要很长时间)。或者,如果它看起来很有趣并且您想保存以备后用怎么办?也许您应该在新的背景选项卡中打开它,然后继续阅读当前页面。或者,您可能决定已阅读完此页面,因此只需点击当前选项卡中的链接即可。

关键是,您有自己的工作流程,并且您希望浏览器有相应的行为。如果它为您做出这些决定,您可能会感到非常沮丧。

THAT being said, web developers should make it absolutely clear where their links go, what types and/or formats of sources they reference, and what they do. Tooltips can be your friend (unless you're using a tablet or phone; in that case, specify these on the mobile site). We all know how much it sucks to be taken somewhere we weren't expecting or make something happen we didn't mean to.

话虽如此,Web 开发人员应该绝对清楚他们的链接在哪里,他们引用的资源类型和/或格式,以及他们做什么。工具提示可以成为您的朋友(除非您使用的是平板电脑或手机;在这种情况下,请在移动网站上指定这些提示)。我们都知道被带到我们没有预料到的地方或让我们无意中发生的事情是多么糟糕。

回答by Homer

it's by the easiest way to open a new window for something like a PDF

这是为 PDF 之类的文件打开新窗口的最简单方法

It's also the easiest way to annoy non-Windows users. PDF open just fine in browsers on other platforms. Opening a new window also messes up the navigation history and complicates matter on smaller platforms like smartphones.

这也是惹恼非 Windows 用户的最简单方法。PDF 在其他平台上的浏览器中打开得很好。打开一个新窗口也会弄乱导航历史,并使智能手机等较小平台上的问题复杂化。

Do NOT open new windows for things like PDF just because older versions of Windows were broken.

不要因为旧版本的 Windows 损坏而为 PDF 之类的东西打开新窗口。

回答by Vamshi Krishna

Most web developers use target="_blank"only to open links in new tab. If you use target="_blank"only to open links in a new tab, then it is vulnerable to an attacker. When you open a link in a new tab ( target="_blank"), the page that opens in a new tab can access the initial tab and change its location using the window.openerproperty.

大多数 Web 开发人员target="_blank"仅用于在新选项卡中打开链接。如果您target="_blank"仅用于在新选项卡中打开链接,那么它很容易受到攻击者的攻击。当您在新选项卡 ( target="_blank") 中打开链接时,在新选项卡中打开的页面可以访问初始选项卡并使用window.opener属性更改其位置。

Javascript code:

Javascript代码:

window.opener.location.replace(malicious URL)

Prevention:

预防:

rel="nofollow noopener noreferrer"

回答by kojow7

While target is still acceptable in HTML5 it is not preferred. To link to a PDF file use the downloadattribute instead of the target attribute.

虽然目标在 HTML5 中仍然可以接受,但它不是首选。要链接到 PDF 文件,请使用下载属性而不是目标属性。

Here is an example:

下面是一个例子:

<a href="files/invoice.pdf" download>Invoice</a>

If the original file name is coded for unique file storage you can specify a user-friendly download name by assigning a value to the download attribute:

如果原始文件名被编码用于唯一的文件存储,您可以通过为下载属性指定一个值来指定用户友好的下载名称:

<a href="files/j24oHPqJiUR2ftK0oeNH.pdf" download="invoice.pdf">Invoice</a>

Keep in mind that while most modern browsers support this feature some may not. See caniuse.comfor more info.

请记住,虽然大多数现代浏览器都支持此功能,但有些可能不支持。有关更多信息,请访问 caniuse.com

回答by Johan Stiven Hernandez Osorio

You can do it in the following way with jquery, this will open it in a new window:

您可以通过以下方式使用 jquery 进行操作,这将在新窗口中打开它:

<input type="button" id="idboton" value="google" name="boton" /> 

<script type="text/javascript">
    $('#idboton').click(function(){
        window.open('https://www.google.com.co');
    });

</script>

回答by Erik Bi

I think target attribute is deprecated for the <link>element, not <a>, that's probably why you heard it's not supposed to be used anymore.

我认为<link>元素不推荐使用 target 属性,而不是<a>,这可能就是您听说不应再使用它的原因。