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

How to Open Hyperlinks in New Windows/Tabs Using JavaScript?

Mary-Kate Olsen
Release: 2024-10-21 21:22:03
Original
199 people have browsed it

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";
}
Copy after login

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
  );
}
Copy after login

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!

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!