Code to open a link in new tab
To open a link in a new tab, you can use the following code:
HTML
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Open in new tab</a>
The target
attribute is set to _blank
, which tells the browser to open the link in a new tab or window.
JavaScript
var link = document.createElement('a');
link.href = 'https://www.example.com';
link.target = '_blank';
document.body.appendChild(link);
This code creates a new <a>
element, sets its href
attribute to the desired URL, and sets its target
attribute to _blank
. Finally, it appends the element to the <body>
of the page.
CSS
a[target="_blank"] {
target: _blank;
}
This code uses a CSS selector to target all <a>
elements with a target
attribute set to _blank
, and sets the target
attribute to _blank
for those elements.
Note that the target
attribute can also be set to _parent
to open the link in the parent frame, or _self
to open the link in the same frame.