Open a URL in a new tab (and not a new window)
Nothing an author can do can choose to open in a new tab instead of a new window; it is a user preference. (Note that the default user preference in most browsers is for new tabs, so a trivial test on a browser where that preference hasn't been changed will not demonstrate this.)
CSS3 proposed target-new, but the specification was abandoned.
The reverse is not true; by specifying certain window features for the window in the third argument of window.open()
, you can trigger a new window when the preference is for tabs.
How to open link in a new tab in HTML?
Set the target
attribute of the link to _blank
:
<a href="#" target="_blank" rel="noopener noreferrer">Link</a>
For other examples, see here: http://www.w3schools.com/tags/att_a_target.asp
Note
I previously suggested blank
instead of _blank
because, if used, it'll open a new tab and then use the same tab if the link is clicked again. However, this is only because, as GolezTrol pointed out, it refers to the name a of a frame/window, which would be set and used when the link is pressed again to open it in the same tab.
Security Consideration!
The rel="noopener noreferrer"
is to prevent the newly opened tab from being able to modify the original tab maliciously. For more information about this vulnerability read the following articles:
- The target="_blank" vulnerability by example
- External Links using target='_blank'
Open link in new tab or window
You should add the target="_blank"
and rel="noopener noreferrer"
in the anchor tag.
For example:
<a target="_blank" rel="noopener noreferrer" href="http://your_url_here.html">Link</a>
Adding rel="noopener noreferrer"
is not mandatory, but it's a recommended security measure. More information can be found in the links below.
Source:
- MDN | HTML element
<a>
| attributetarget
- About rel=noopener
- Opens External Anchors Using rel="noopener"
Make a link open a new window (not tab)
With pure HTML you can't influence this - every modern browser (= the user) has complete control over this behavior because it has been misused a lot in the past...
HTML option
You can open a new window (HTML4) or a new browsing context (HTML5). Browsing context in modern browsers is mostly "new tab" instead of "new window". You have no influence on that, and you can't "force" modern browsers to open a new window.
In order to do this, use the anchor element's attribute target
[1]. The value you are looking for is _blank
[2].
<a href="www.example.com/example.html" target="_blank">link text</a>
JavaScript option
Forcing a new window is possible via javascript - see Ievgen's excellent answer below for a javascript solution.
(!) However, be aware, that opening windows via javascript (if not done in the onclick event from an anchor element) are subject to getting blocked by popup blockers!
[1] This attribute dates back to the times when browsers did not have tabs and using framesets was state of the art. In the meantime, the functionality of this attribute has slightly changed (see MDN Docu)
[2] There are some other values which do not make much sense anymore (because they were designed with framesets in mind) like _parent
, _self
or _top
.
JavaScript: How to open a link in a new tab without window.open()?
I believe, the "noopener" feature runs the window in a seperate process
https://developer.mozilla.org/en-US/docs/Web/API/Window/open#noopener
let d = window.open('https://www.google.com/', 'name', 'noopener=yes');
with noopener, d would now be null
Open link in new window , not in new tab
Try like
<li onclick="window.open('http://www.google.com','mywindow','height:400;width:400;')">Demo</li>
Open every link in new tab
Are you looking for a CSS solution only? or any solution like the one below (JQuery)
$('a').click(function() {
$(this).attr('target', '_blank');
});
And also you can add the code below in your head
tag-
<base target="_blank">
this will make all the a
tags open in a new tab
Related Topics
How to Make Div Elements Display Inline
How to Make a Vertical Line in Html
Why Does HTML Require That Multiple Spaces Show Up as a Single Space in the Browser
Why Is 'Position: Sticky' Not Working With Core Ui'S Bootstrap Css
Why Is Vertical-Align: Middle Not Working on My Span or Div
What Is the Effect of Encoding an Image in Base64
Svgs Not Scaling Properly in Ie - Has Extra Space
Absolute Urls Omitting the Protocol (Scheme) in Order to Preserve the One of the Current Page
The Reference to Entity "Foo" Must End With the ';' Delimiter
How to Submit Form on Change of Dropdown List
How to Draw Vertical Text With CSS Cross-Browser
How to Choose Between 'Class' and 'Id'
Which Characters Need to Be Escaped in Html
Another: Force Chrome to Fully Buffer Mp4 Video
How to Fallback to Local Stylesheet (Not Script) If Cdn Fails