Home > Article > Web Front-end > How to Open Hyperlinks in New Windows/Tabs Using JavaScript?
Opening Links in New Windows/Tabs with JavaScript
When working with third-party JavaScript files, you may encounter links that redirect to target pages and replace the current one. To resolve this and open the target page in a new tab, consider using the window.open() method.
In your code, you currently have:
if (command == 'lightbox') { location.href="https://support.wwf.org.uk/earth_hour/index.php?type=individual"; }
This code redirects the current page to the target URL when command equals 'lightbox'. To open the target page in a new tab, replace the location.href assignment with window.open():
if (command == 'lightbox') { window.open( 'https://support.wwf.org.uk/earth_hour/index.php?type=individual', '_blank' // Parameter that specifies opening in a new window ); }
The '_blank' parameter in window.open() opens the target link in a new window or tab, depending on the user's browser settings. This allows you to create new tabs while preserving the original content.
The above is the detailed content of How to Open Hyperlinks in New Windows/Tabs Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!