1

I have a page that can be opened from either my domain or as a popup opened from a different domain. In this page, I have to check it by Javascript if it was opened as a popup or if it was directly opened by it's URL and do some stuff.

Right now, I'm following the widely suggested approach, using the window.opener attribute...

if (window.opener != null)
    // It is a popup
else
    // It's not a popup

... but recently I've been facing an IE issue, which isn't filling up that attribute.

Could anyone suggest me a different approach to accomplish this? The IE issue is probably unavoidable without changing some windows configurations.

Btw, when I say that the page is being opened as a popup, I'm talking about a window.open() call that opens a new window.

Thank you in advance!

4
  • 1
    I'm just going to go ahead and ask the obvious: Why do you need to know if the window is popup? What is different?
    – Halcyon
    Nov 19, 2013 at 17:02
  • I need to do some layout changes in both cases. I know, it's messy.
    – everton
    Nov 19, 2013 at 17:08
  • 1
    I don't know of a reliable way to do this. If you could modify the JS code that opens the popup then maybe. Can you modify the URL or some query parameter? IE. my.domain.com/index.php?opener=popup
    – Halcyon
    Nov 19, 2013 at 17:11
  • Nope, I guess that's not an option.
    – everton
    Nov 19, 2013 at 17:36

1 Answer 1

1

Check document.referrer inside the popup.

var ref = document.referrer;
if (ref.match(/mydomain/i)) {
    // page was opened directly
}
else {
    // page was opened by other domain
    // this is a popup
}
7
  • Could you elaborate please. How do you use the referrer to detect if a window is a popup?
    – Halcyon
    Nov 19, 2013 at 17:02
  • @FritsvanCampen the pertinent detail seems to be not whether it is a popup but rather whether it was opened from OP's domain or some other. The referrer is the source of that information, no?
    – Evan Davis
    Nov 19, 2013 at 17:04
  • Are you talking about checking if it's a cross domain call? That's not what I'm asking...
    – everton
    Nov 19, 2013 at 17:04
  • 1
    @EvertonAgner it sounds like, in your use case, cross-domain and popup are the same scenario. Is there a third (or fourth) scenario where the page can be opened as a popup from your domain or directly by the other domain?
    – Evan Davis
    Nov 19, 2013 at 17:07
  • 2
    If the page is opened from another domain it's useful to note that most browsers don't pass the referrer anymore; to improve user privacy.
    – Halcyon
    Nov 19, 2013 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.