jQuery Cleanly Open Links in Popup Windows

By Sam Deering

Simple jQuery code snippet to add an event to all links with class .popup to open in a popup window instead of the current page or new tab/window. Specify the heights and widths as you please.

jQuery(document).ready(function($) {
	jQuery('a.popup').live('click', function(){$(this).attr('href'),'','height=200,width=150');
		if (window.focus) {newwindow.focus()}
		return false;
  • Austin

    Hi jquery4u,

    I found this script quite lightweight and useful. I was wondering if there was a way to not only open the link in the popup, but also center the popup? If not center, otherwise position the popup?

    Any help would be greatly appreciated.

  • jquery4u

    Hi Austin,

    Because is a built in JavaScript method it doesn’t have positioning available (that i am aware).

    As you say this script is very lightweight so if you wanted more options for popup windows have a look here:

  • Razvan

    Yes, it does open the link in a new pop-up window, but it also opens it in the current page (link’s page). :( Why?

  • Gurumoorthi S is my blog. I want to create a link which is go to 2 link. one is opened in self and another is opened in new window. I done it. But i want the existing window with focus instead new window. My code is not working in firefox and chrome. Can you solve my proplem

  • Javed Ur Rehman

    This piece of code is very helpful for me :) thanks Sam Deering great work.

    • jQuery4u

      Your very welcome :)



Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.