0 day streak
javascript / beginner
Snippet

Securing External Hyperlinks with Safe Target Attributes

Opening external links in a new browser tab with target="_blank" can expose apps to security vulnerabilities (tabnabbing). Adding rel="noopener noreferrer" prevents the target page from accessing window.opener and blocks referrer leakage.

snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
function SafeExternalLink({ href, text }) {
return (
<a
href={href}
target="_blank"
rel="noopener noreferrer"
>
{text}
</a>
);
}
react
Breakdown
1
function SafeExternalLink({ href, text }) {
Creates a reusable wrapper component for outbound hyperlinks.
2
target="_blank"
Instructs the browser to open the target destination in a new browser tab or window.
3
rel="noopener noreferrer"
Security attribute that isolates the new tab context and suppresses the Referer HTTP header.