Home > Web Front-end > JS Tutorial > body text

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

Patricia Arquette
Release: 2024-10-21 21:19:02
Original
228 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!