HTML - open link in new tab
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.
Ā