Alternative to check if the window is a popup

12

I have a page that can be opened both by my own domain and externally, both as a "normal" window as a popup.

In this window, I need to check whether it has been opened as a popup or not, and perform some customizations programmatically. So far, I've been using the following approach (widely recommended):

if (window.opener)
    // É um popup
else
    // Não é um popup

However, I have found Compatibility issues with Internet Explorer , which causes the opener attribute to not be filled.

Could anyone suggest an alternative to do this check?

Note:

  • In general, I can not change the code that opens the window (eg to include querystring parameters)
  • When I say open as a popup, I mean a call to window.open() .
asked by anonymous 30.01.2014 / 15:59

3 answers

6

The window.open method returns the window object that was opened, so if it is in the same domain I believe you can add an attribute to the popup window object after opening and checking it.

Page that opens the popup:

var pop = window.open("http://www.dominio.com");
pop.ePopup = true;

Popup:

if (typeof window.ePopup !== 'undefined' && window.ePopup)
    // É um popup
else
    // Não é um popup

I'm not sure about the compatibility of this solution. And if you are in separate domains you will have problem of CORS that can be solved in the server configuration most of the time.

    
30.01.2014 / 16:14
1

I found this information in msdn is a security issue indeed.

I would use the approach when it is necessary to open the popup I would put in a Dialog using jqueryui or another framework.

Open popup is complicated, try to avoid as much as you have the issue of popup blockers installed in browsers.

    
30.01.2014 / 16:18
-1

MDN Direct:

window.opener

Usage:

var parentWindow = window.opener;

Make sure the window.opener property is null, if it is, because it was not opened by another window, if it is not null, then it was opened by another window.

It's worth noting that this is not part of any standard ... as the MDN itself says.

Note: As explained in this post , some browsers may end up setting the property to zero if the parent window is closed. In this case, I recommend that the verification be done as soon as the popup page is opened, and the value stored in any variable.

    
06.02.2014 / 21:30