Alternative to check if the window is a popup

Asked

Viewed 918 times

12

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

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

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

Yet I have found compatibility problems with the Internet Explorer, which causes the attribute opener is not completed.

Could someone tell me some alternative to perform this check?

Obs:

  • In general, I cannot change the code that opens the window (e.g., to include querystring parameters)
  • When I say open as popup, I mean a call to window.open().
  • Have you considered using the header HTTP Referrer to verify the source of the request on the server and then, for example, redirect or include a script according to the source?

  • @utluiz Unfortunately, it is not the referrer who defines the way the window is being opened (popup or not)... I included it in the explanation just to make the scenario better understood.

  • Right. Were you able to test Erik’s response? It worked?

  • @utluiz Yes, but anyway it will not be possible to follow with this approach, because this would imply in changing all the places that are opening this popup, and many of them are from other third systems.

  • Maybe my answer to use window.opener work. =)

3 answers

6

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

Tab 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 am not sure about the compatibility of this solution. And if you are in separate domains you will have problem of CORS which can be solved in server configuration most of the time.

  • Unfortunately, this would involve changes to the code that opens the popup, and part of it is third-party domain. For that same reason, could not use querystring parameters also.

1

I found this information on msdn is a matter of security even.

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

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

  • Thank you for the reference of the problem in the MSDN, I will include in the question :)

-1

Direct from the MDN:

window.opener

Use:

var parentWindow = window.opener;

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

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

Note: As explained in this post in English, some browsers may end up setting the property null if the mother 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.

  • Sorry, but I mentioned in the question that I already use this attribute but I have found problems with IE.

  • @Evertonagner As you said the value is not being filled in, I suggested that you read the value as soon as possible, and store in a variable whether or not it is a popup... this also doesn’t work?

  • The problem is that IE’s default security settings typically generate some blocks in cross-Omain requests. One of them is never fill the opener attribute.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.