Javascript open new tab
A simple but useful task!
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:
<button onclick="openNewTab()">Open New Tab</button>
<script>
function openNewTab() {
window.open("https://www.example.com", "_blank");
}
</script>
When you click the button, the openNewTab()
function will be called, which 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", "toolbar=yes,location=yes");
- sets the toolbar and location bar to be visible in the new tab.
Note that the window.open()
method returns a reference to the new window, which you can use to interact with the new tab.
Alternatively, you can use the window.open()
method with the target
attribute set to _blank
in an anchor tag:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Open New Tab</a>
This will also open a new tab when the link is clicked.