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.
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(); } }); });
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!