Opening URLs in New Tabs Without Pop-up Windows
When attempting to open a URL in a new tab, many developers encounter the issue of the browser opening a pop-up window instead. Despite using code snippets such as window.open(url, '_blank'); and window.open(url);, the desired behavior is not achieved.
Solution
To resolve this problem, a clever technique can be employed:
function openInNewTab(url) { window.open(url, '_blank').focus(); } // Or just window.open(url, '_blank').focus();
This code focuses the newly opened tab, ensuring that the browser opens the URL in a tab rather than a window.
To utilize this solution effectively, it's recommended to implement it directly in the onclick handler for the link. This prevents pop-up blockers and the default "new window" behavior.
<div onclick="openInNewTab('www.test.com');">Something To Click On</div>
Reference
For further details, refer to the resource: "Open a URL in a new tab using JavaScript."
The above is the detailed content of How Can I Open URLs in New Tabs Instead of Pop-up Windows Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!