How to Open Links in a Popup Window

Use a popup window only when it serves a clear purpose, such as showing a focused preview or a small tool alongside the current page. For ordinary navigation, a regular link is more predictable: browsers and users control whether a new browsing context appears as a tab or a separate window.

The JavaScript window.open() method can request a popup, but it cannot guarantee the browser's layout, size, or placement. Popup blockers may also reject a request. This guide shows a user-initiated example with a normal link as a fallback.

Open a link in a popup

Keep a real destination in the link's href, then enhance its click with window.open(). Without JavaScript, the link remains ordinary navigation in a new browsing context. The separate fallback link remains available if a browser blocks the popup.

<a
  id="popup-link"
  href="https://kanishkkunal.com/"
  target="_blank"
  rel="noopener"
>
  Open the site in a popup window
</a>

<p>
  Popup blocked?
  <a href="https://kanishkkunal.com/" target="_blank" rel="noopener">
    Open the site in a separate browsing context
  </a>.
</p>

<script>
  document.querySelector("#popup-link").addEventListener("click", (event) => {
    event.preventDefault();

    window.open(
      event.currentTarget.href,
      "_blank",
      "popup,width=600,height=600,noopener"
    );
  });
</script>

Replace the example URL with the page you want to open. Put the script in a JavaScript file loaded after the link, or use a deferred script. The click listener calls window.open() directly during the user's action; delaying it with a timer or waiting for an unrelated asynchronous operation can cause browsers to block it.

The popup and width/height values are requests, not guarantees. A browser may use a tab, enforce minimum dimensions, ignore some features, or apply its own interface. Mobile browsers in particular may handle the request differently. Avoid relying on exact placement or on older flags such as scrollbars=no and resizable=no; users need to be able to resize, scroll, and control their browsing context.

Handle popup blocking and security

Keep the regular link visible rather than trying to silently work around a popup blocker. The noopener feature prevents the opened page from accessing the page that opened it through window.opener. When noopener is used, window.open() can return null even when the new context opened, so that return value is not a reliable way to distinguish success from a blocked popup. See the current MDN documentation for Window.open() and MDN's reference for the noopener link relation.

The popup request must be made in response to a direct user action, such as the link click above. Do not open popups automatically on page load, or launch several windows from one click. Explain that the link opens a new window, and retain a normal navigation option so keyboard, touch, and assistive-technology users are not forced into an unpredictable interaction.

Popup window example

This older screenshot shows the kind of separate window the example requests. Browser controls and popup behavior vary, so the current result may look different.

Example of a popup window; appearance varies by browser
Example popup window

Popup window or new tab?

Use a popup only when a separate, focused window is part of the task. For a normal external destination, a standard link is usually the better choice:

<a href="https://kanishkkunal.com/" target="_blank" rel="noopener">
  Open the site in a new browsing context
</a>

target="_blank" asks the browser to open another browsing context; it does not promise a tab rather than a window. The rel="noopener" value prevents the destination from controlling the original page. If the destination should open in the current tab, omit target="_blank".

Use popups sparingly, make the destination clear before activation, and leave the final choice of window behavior to the browser and the person using it.

22 thoughts on “How to Open Links in a Popup Window”

  1. Hi,

    Thank you for those examples.. it works really great.

    I would like to know if it’s possible by copying/pasting link in a new window to display the modal window ?
    For example.. if you have a website with some blog posts on homepage that launch in a modal window when you click on it, is it possible to open exactly the same way the blog post when you access to it via the url (www.mydomain.com/post_1) ?

    Thank for the help !

    Reply
    • Hi Wesly,
      While I don’t understand why you would want to do that, you may try to add the following script to the current page for opening it in a popup window.

      window.open(window.location.href, ‘popup’, ‘width=600,height=600’);

      Reply
      • Hi, thank you for your quick response.

        In fact, this is not what I’m looking for ^^ I think I have expressed myself badly. Here is a screenshot to the website i am developped on WordPress right now (http://i.imgur.com/kLsuZs2.png)… maybe this will help understand me ? I have some posts listed one after the other (thumbnail + title + excerpt). When I click on it, a modal window open with the full post content (title + the content). All works fine. But, when i tried to access to my full post directly with the url, it open me the single.php file (http://i.imgur.com/qtOwreR.png).

        I would like to have the homepage and the post in the modal window. Hope you can help.

        Thank you very much

        Reply
        • Hi Wesley, the modal window that is visible in your screenshot is a custom overlay and not a popup window that get opened when using window.open().
          What I mean to say that this is not the popup I have written about in my post.
          There is possibly a lightbox kind of script running on your website which is opening your post in an overlay inside the same window.

          Reply
  2. it not works if block pop up turn on, is there any other option to avoid it? i am using firefox, and i tried example above but there’s no different with open in new window.

    Reply
    • Hi andy,
      The code above works fine on Firefox. However, if you have a pop-up blocker installed then it may have a setting to open any pop-up in a new tab and as far as I know, nothing can enable you to bypass that. You either have to turn off that setting in your popup blocker or disable the popup blocker itself.

      Reply
  3. Hi, I am trying to open the popup through a button and not through the words Open Link in Popup. I tried replacing it with the html code for the button but it would not open the popup. Any tips?

    Reply

Leave a Reply