The target attribute of the link
Basic Concepts of the target Attribute
The target
attribute in HTML is used to specify how a link or form submission result should be opened. It commonly appears in <a>
, <area>
, <form>
, and <base>
tags, controlling where new content is displayed after a user clicks a link. This attribute plays a significant role in web navigation and user experience design.
<a href="https://example.com" target="_blank">Open in new window</a>
Common Values of the target Attribute
_blank
The most frequently used target value, indicating that the link should open in a new window or tab:
<a href="page.html" target="_blank">Open in new window</a>
Modern browsers typically open links in a new tab rather than a new window. This behavior can be configured in browser settings.
_self
The default value, indicating that the link should open in the current window/frame:
<a href="page.html" target="_self">Open in current window</a>
<!-- Equivalent to -->
<a href="page.html">Open in current window</a>
_parent
Opens the link in the parent frameset. If there is no parent frame, it behaves the same as _self
:
<a href="page.html" target="_parent">Open in parent frame</a>
_top
Opens the link in the topmost frame of the window, canceling all frames:
<a href="page.html" target="_top">Open in top frame</a>
Custom Frame Name
A specific frame or window name can be specified:
<a href="page.html" target="contentFrame">Open in specified frame</a>
If the specified window/frame does not exist, the browser will create a new window and assign it that name.
Advanced Usage of the target Attribute
Using with iframe
<iframe name="contentFrame" src="initial.html"></iframe>
<a href="newpage.html" target="contentFrame">Load in iframe</a>
Multi-Window Control
// Open a new window and retain a reference
const newWindow = window.open('', 'myWindow', 'width=600,height=400');
document.querySelector('a').onclick = function() {
newWindow.location.href = 'newpage.html';
return false;
};
Form Submission Target
<form action="submit.php" target="responseFrame">
<!-- Form content -->
</form>
<iframe name="responseFrame"></iframe>
Security Considerations for the target Attribute
Preventing Phishing Attacks
When using target="_blank"
, the newly opened page can access the original page via window.opener
, posing a security risk:
<!-- Unsafe method -->
<a href="https://malicious.com" target="_blank">Click here</a>
<!-- Safe method -->
<a href="https://malicious.com" target="_blank" rel="noopener noreferrer">Secure link</a>
noopener and noreferrer
Modern browsers recommend using rel="noopener noreferrer"
:
<a href="external.html" target="_blank" rel="noopener noreferrer">Secure external link</a>
Browser Compatibility of the target Attribute
All major browsers fully support the target attribute, but behavior may vary on mobile devices:
- iOS Safari may ignore certain target settings
- Some browser extensions may modify default behavior
- Pop-up blockers may prevent certain
target="_blank"
openings
CSS Selectors for the target Attribute
CSS can be used to style links with specific target attributes:
a[target="_blank"]::after {
content: "↗";
font-size: 0.8em;
}
a[target="contentFrame"] {
background-color: #f0f0f0;
}
Interaction Between target and JavaScript
JavaScript can dynamically control target behavior:
// Dynamically modify target
document.querySelectorAll('a').forEach(link => {
if (link.href.startsWith('http')) {
link.target = '_blank';
link.rel = 'noopener noreferrer';
}
});
// Detect target attribute
const link = document.getElementById('myLink');
if (link.target === '_blank') {
console.log('This link will open in a new window');
}
Special Handling of target in Single-Page Applications (SPA)
Frameworks like Vue and React may require special handling for target:
// React example
function ExternalLink({ href, children }) {
return (
<a href={href} target="_blank" rel="noopener noreferrer">
{children}
</a>
);
}
SEO Impact of the target Attribute
Search engine crawlers typically ignore the target attribute, but there are some considerations:
- Excessive use of
target="_blank"
may affect user experience - Ensure external links include
rel="nofollow"
orsponsored
attributes - Internal navigation is best served with the default
target="_self"
Alternatives to the target Attribute
In some cases, JavaScript can replace the target attribute:
document.getElementById('myLink').addEventListener('click', function(e) {
e.preventDefault();
window.open(this.href, 'newwindow', 'width=800,height=600');
});
Future Trends of the target Attribute
HTML5 introduced some new related features:
<base target="_blank">
can set a default target for all links on a page- Proposed
target="modal"
for native dialog boxes - Enhanced iframe communication APIs may influence how target is used
Practical Use Cases of the target Attribute
Multiple Document Interface (MDI) Applications
<nav>
<a href="doc1.html" target="content">Document 1</a>
<a href="doc2.html" target="content">Document 2</a>
</nav>
<iframe name="content" src="welcome.html"></iframe>
Split-View Reading Mode
<a href="article.html" target="reader" class="split-view">Reading Mode</a>
<!-- Implement split-screen effects via CSS and JS -->
Embedded Help System
<a href="help.html#topic1" target="helpWindow">Help</a>
<script>
window.addEventListener('load', function() {
const helpLinks = document.querySelectorAll('a[target="helpWindow"]');
helpLinks.forEach(link => {
link.onclick = function() {
window.open(this.href, 'helpWindow', 'width=400,height=600,left=100,top=100');
return false;
};
});
});
</script>
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:页面内锚点的创建与使用
下一篇:链接的title属性