Home  >  Article  >  Web Front-end  >  How to Open Links in a New Window/Tab Using JavaScript\'s \'window.open()\'?

How to Open Links in a New Window/Tab Using JavaScript\'s \'window.open()\'?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-21 21:19:02231browse

How to Open Links in a New Window/Tab Using JavaScript's 'window.open()'?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn