Languages
[Edit]
EN

HTML - open link in new tab

6 points
Created by:
Elias999
759

In this article, we would like to show you how in a safe way create a link that isĀ openedĀ after a click inĀ a new tab.

Quick solution:

// ONLINE-RUNNER:browser;

<a href="https://some-website.com/path" rel="noopener" target="_blank">
  Link name
</a>

Note (inĀ 2021): when we useĀ target="_blank"Ā attribute in links, in major modern web browsers, it is automatically usedĀ rel="noopener"Ā attribute behaving to prevent vulnerabilities - check notificationĀ here.

Warning: for security reasons, it is important to use rel="noopener" attribute, even if some tools warns us about unnecessary attribute.

or:

// ONLINE-RUNNER:browser;

<!-- WARNING: use this solution when you open the page that is safe, e.g. your pages -->

<a href="https://safe-website.com/path" target="_blank">
  Link name
</a>

Hint:Ā rel="noopener" is not needed if we know the link opens the safe pageĀ (safe page is the page that will not make unsafe opetaion on out web page using JavaScript).

Ā 

Practical example

In this example, we use a element with additional properties to open the link in a new tab:

  • target="_blank"Ā - specifies to open the linked pageĀ in a new tab,
  • rel="noreferrer noopener" - which is a good practice and helpsĀ to preventĀ tabnabbingĀ and hide information about from what page the user came.
// ONLINE-RUNNER:browser;

<a href="https://dirask.com/findings" rel="noreferrer noopener" target="_blank">
  Check out Dirask findings!
</a>

Note: the default value of the target property is _self which opens linked page in the current working tab.

Ā 

References

  1. Link type "noreferrer" - W3C Working Draft
  2. Link type "noopener" - W3C Working Draft

Alternative titles

  1. HTML - open link in new window
  2. HTML - safe way to open link in new window
  3. HTML - safe way to open link in new tab
  4. HTML - noopener example
  5. HTML - noreferrer example
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join