Javascript to open 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.
Here's an example:
<a rel="nofollow" target="_blank" href="#" onclick="window.open('https://www.example.com', '_blank')">Open in new tab</a>
This will open the URL https://www.example.com
in a new tab when the link is clicked.
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", "toolbar=no,menubar=no,scrollbars=yes");
- sets various attributes of the new tab, such as whether to display the toolbar or menubar.
Note that the window.open()
method returns a reference to the new window, which you can use to interact with the new tab programmatically.
Alternatively, you can use the target
attribute on an <a>
tag to achieve the same effect:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Open in new tab</a>
This is a more straightforward way to open a new tab, but it's not as flexible as using the window.open()
method.