Javascript open url in new tab
A simple one!
To open a URL in a new tab using JavaScript, you can use the window.open()
method or the location.href
property.
Method 1: Using window.open()
window.open('https://www.example.com', '_blank');
This will open the URL https://www.example.com
in a new tab.
Method 2: Using location.href
location.href = 'https://www.example.com';
This will open the URL https://www.example.com
in the current tab. To open it in a new tab, you can use the target
attribute:
location.href = 'https://www.example.com';
location.href = 'https://www.example.com'; // Open in new tab
Method 3: Using an anchor tag
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Open in new tab</a>
This will create an anchor tag that opens the URL https://www.example.com
in a new tab when clicked.
Note that in all cases, the URL should be a valid URL that can be opened in a browser.
Also, keep in mind that some browsers may have restrictions on opening new tabs or windows, so it's always a good idea to check the browser's settings and user preferences before attempting to open a new tab.