If you've ever used target="_blank" in HTML, you know it opens a link in a new tab or window. But have you ever wondered why the underscore is needed? Let’s break it down.
The Target Attribute Explained
The target attribute in an <a> tag specifies where to open the linked document. Here are its main values:
- _blank: Opens the link in a new window or tab.
- _self: Opens the link in the same frame (default).
- _parent: Opens the link in the parent frame.
- _top: Opens the link in the full body of the window.
- <framename>: Opens the link in a named frame.
A Bit of History: Frames and Naming
Before HTML5, developers used <frameset> tags to divide a browser window into multiple frames. Each frame had a unique ID, like id="sidebar" or id="content". The target attribute allowed links in one frame to load content into another frame:
<a href="/pricing" target="content"></a>
Here, clicking the link would load the /pricing page in the frame named "content."
Now, if you used:
<a href="/" target="blank"></a>
The browser would look for a frame named "blank." If none existed, it would open a new tab and assign it the name "blank." Clicking the same link again would reuse that tab because the browser had already named it.
Why the Underscore?
The underscore (_) was introduced to differentiate special values from user-defined frame names. For example, _blank explicitly tells the browser to open a new tab every time, without reusing an existing one. Similarly, _self, _parent, and _top are reserved keywords, each serving a specific purpose.
Modern Best Practices
Since <frameset> is deprecated in HTML5, there’s usually no need to name frames. Instead, use _blank for opening new tabs. Just remember to include rel="noopener noreferrer" for security and performance reasons:
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Visit Example</a>
This prevents the new tab from accessing the window.opener object of the original page, safeguarding against potential security vulnerabilities.
Conclusion
The underscore in target="_blank" exists to distinguish built-in behaviors from custom frame names. Although frames are mostly a thing of the past, this convention remains essential for maintaining compatibility and ensuring predictable browser behavior.
P.S. Don’t use <frameset>—it's deprecated in HTML5.