Javascript open a new tab
A simple one!
To open a new tab in JavaScript, you can use the window.open()
method. Here's the basic syntax:
window.open(url, "_blank");
Where:
url
is the URL you want to open in the new tab._blank
is the target attribute that specifies the new tab.
For example:
window.open("https://www.example.com", "_blank");
This will open a new tab with the URL https://www.example.com
.
You can also specify additional options, such as:
window.open(url, "_blank", "width=500,height=300");
- sets the width and height of the new tab.window.open(url, "_blank", "noopener");
- sets thenoopener
attribute to prevent the new tab from accessing the parent window.
Note that window.open()
returns a reference to the new window, which you can use to interact with the new tab.
Alternatively, you can use the target
attribute on an <a>
tag to open a new tab:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Open in new tab</a>
This will also open a new tab when the link is clicked.