How to Open Links in a Popup Window

Popup windows are not always desirable and are most likely frowned upon by many due to it being infamous for being used to open advertisements. However there may be a genuine need for opening links in a popup window in web applications. In this post, I will show how we can easily define click behavior on links to open them in a popup along with some customization options such as sizing the popup window.

Normally links get opened in the same window in which they are clicked in. In order to open them in a new window, we add target="_blank" attribute to links. However to open the links in a separate popup window, we can make use of the onclick property and specifying a inline JavaScript code window.open as shown below.

HTML + JavaScript code for Popup

<a href="http://kanishkkunal.in" 
  target="popup" 
  onclick="window.open('http://kanishkkunal.in','popup','width=600,height=600'); return false;">
    Open Link in Popup
</a>

Here we are asking the browser to add our inline JavaScript code in the click behavior of the link by specifying the onclick attribute. We are passing the URL to be opened in popup along with the width and height of the popup window that we want to create. By adding return false we are ensuring that the default click action is not executed.

Demo of Popup Window

Click on the button below to see the above code in action. A popup window of size 600×600 should get created and it should open the link that we specified.


Open Link in Popup

While this kind of popup should not be blocked by the browser as we are opening just one new window on user click, however if your browser is over-aggressive in blocking popups then you may need to temporarily disable the popup blocker in order to see it in action.

As seen in the screen-clip below, the popup window that we created will be re-sizable by the user and will also have its own address-bar as well as the scrollbar.

Opening Link in Popup
Resizable Popup Window with Scrollbars

Having the popup window re-sizable and scrollable by the user is the most ideal case when you are creating popups. Not being able to resize or scroll a popup window might frustrate some users. However if you have a specific use case where you want to disable scrolling as well as resizing on the popup window then read on to do these customizations.

Customizing Popup Window

In the most basic form as we saw above, the popup window will allow users to resize, scroll and change the address of it. In certain cases, you may want to disallow such actions on the popup window. In order to achieve that, we need to pass on additional parameters to the window.open method. These additional parameters that can be used are as follows:

location, menubar, resizable, scrollbars ,status, titlebar and toolbar (source)

Note that not all of these parameters are supported by all the browsers and therefore you may not have a uniform behavior if you specify them. For e.g. lets see a use-case below

Disable resizing and scrolling in popup window

To disable resizing and scrolling in the popup window, specify resizable=no and scrollbars=no.

<a href="http://kanishkkunal.in" 
  target="popup" 
  onclick="window.open('http://kanishkkunal.in','popup','width=600,height=600,scrollbars=no,resizable=no'); return false;">
    Open Link in Popup
</a>

Popup with Resizing disabled

You will find that disabling the resizing of popup window is only supported in Internet Explorer and will not work in Chrome or Firefox. Additionally, hiding the scrollbar will work in Internet Explorer (IE), Firefox and Opera but not in Chrome.

Before you go all crazy with popup windows, do take a moment to decide whether you really want to adopt the popup behavior vs opening links in a new window. A link can be opened in a new window (or in a new tab) by simple adding the target="_blank" attribute to links like shown below. These kinds of links are never blocked by the popup blocker while opening.

<a href="http://kanishkkunal.in/" target="_blank">Open in a New Window</a>

To conclude, I would restate that unless you have a very specific reason, opening links in popup window should be avoided as much as possible. But when you really have the need to do it, open just one link at a time and disable the default link behavior in order to avoid popup blockers and to give better experience to the user.

Kanishk Kunal

Kanishk is a Software Engineer turned Online Entrepreneur who has created many successful apps and websites. He devotes most of his time punching his keyboard and swiping his smartphone. Follow him on Twitter @kanishkkunal

This Post Has 16 Comments

  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 !

    1. 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’);

      1. 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 (ex : www.mydomaine.com/article-1), 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

        1. 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.

  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.

    1. 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.

  3. Sir, Is there any way to show adsense content like side bars in all the popups created as mentioned by you?

    1. As far as I know, it is against adSense policy to show ads in popups.

  4. how do i make pop-u coming at center? it always open at left side corner?

    1. Hi Sayan,

      You can specify the left and right attributes to position the popup window.

  5. Sir, i want mouse over pop up.
    thanks in advance.

  6. hi , can you plz tell me how to link a html page as pop up on clicking button with overlay effect?

    1. Hi Rashmi. You can take a look at popup dialog libraries such as https://sweetalert.js.org/

  7. My hero! Thank you!

  8. Thank you

  9. hey how do i make a pop-up link that runs that runs more HTML codes in the pop-up

Leave a Reply

Close Menu