JavaScript: Opening Links in a New Window/Tab Using 'location.href'
Problem:
You have third-party JavaScript code that contains a link that replaces the current page with the target URL. However, you wish to have this link open in a new tab instead.
Original Code:
<code class="javascript">if (command == 'lightbox') { location.href="https://support.wwf.org.uk/earth_hour/index.php?type=individual"; }</code>
Solution:
To have the link open in a new tab, you can use the window.open() method with the '_blank' parameter. Here's a modified version of your code:
<code class="javascript">if (command == 'lightbox') { window.open( 'https://support.wwf.org.uk/earth_hour/index.php?type=individual', '_blank' ); }</code>
By adding '_blank' as the second argument to window.open(), the browser will open the specified URL in a new window.
The above is the detailed content of How to Open Links in a New Window/Tab Using JavaScript\'s \'window.open()\'?. For more information, please follow other related articles on the PHP Chinese website!