Explain Codes LogoExplain Codes Logo

How to make HTML open a hyperlink in another window or tab?

html
responsive-design
web-development
best-practices
Alex KataevbyAlex Kataev·Jan 21, 2025
TLDR

Easily open links in a new tab using the target="_blank" attribute in your <a> tags:

<!-- Making friends with new tabs! --> <a href="https://www.example.com" target="_blank">Example</a>

The quick fix is simply adding target="_blank" to your hyperlink anchor tag. Why complicate things, right?

Deeper dive into 'target' attribute

Grasping the behavior of hyperlinks is essential for web navigation. When a new window or tab is what you seek, introduce the target="_blank" attribute to your <a> tag. Remember though, with great power comes great responsibility. Ensure its usage genuinely enhances user interaction rather than disrupting it.

Likewise, be mindful of popup blockers in modern browsers that might interfere with this tag. Keep the user experience seamless!

Exploring target attribute options

Beyond target="_blank", there are other useful options the target attribute can offer:

  • _self: Default mode; opens the link in the same frame or tab.
  • _parent: Opens the link in the superior frame amidst a nested iframe jungle.
  • _top: Breaks through all nested iframes and starts fresh in a full window.

Wisely using these attributes can enhance engagement and user retention.

Special use cases to consider

There are some special scenarios that demand our attention:

  • Security aware: Use rel=noopener which prevents the new page from accessing your window object via window.opener to enhance security.
<!-- Knock knock...who's there? Safety first! --> <a href="https://www.example.com" target="_blank" rel="noopener noreferrer">Secured Link</a>
  • Accessibility Alert: Screen readers and assistive technologies might announce target="_blank", potentially disorienting users requiring these technologies. Be inclusive!

  • Consistency: If you have multiple links leading to external resources, remember, consistency is key. Open them all in a new tab.