Home > Web Front-end > JS Tutorial > jQuery Cleanly Open Links in Popup Windows

jQuery Cleanly Open Links in Popup Windows

Joseph Gordon-Levitt
Release: 2025-03-05 00:34:08
Original
329 people have browsed it

This jQuery code cleanly opens links with the class "popup" in a new popup window, preventing them from opening in the current page or a new tab. Customize the height and width parameters as needed.

jQuery Cleanly Open Links in Popup Windows

Here's the code:

jQuery(document).ready(function($) {
  jQuery('a.popup').on('click', function(e) {
    e.preventDefault(); // Prevent default link behavior
    const href = $(this).attr('href');
    const newwindow = window.open(href, '', 'height=200,width=150');
    if (newwindow && newwindow.focus) {
      newwindow.focus();
    }
  });
});
Copy after login

This improved version uses on instead of live (which is deprecated) for better event handling and includes e.preventDefault() to reliably prevent the default link action. The check for newwindow adds robustness.

The above is the detailed content of jQuery Cleanly Open Links in Popup Windows. For more information, please follow other related articles on the PHP Chinese website!

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