30

This question is not the duplicate of if window is popup , But a similar one.

I am developing a extension which injects scripts to all web pages. I need to detect whether the window is popup or not.

Note: I am not the one who is opening the popup window, So the above solution won't work.

Community
  • 1
  • 1
Jeevan
  • 3,728
  • 6
  • 27
  • 37

10 Answers10

34

I've found that some browsers will set window.opener to window in certain cases. This is the most reliable popup check that I am using right now.

if (window.opener && window.opener !== window) {
  // you are in a popup
}
Steve
  • 933
  • 7
  • 13
  • Yeah, I think this one is the ticket. – prograhammer Dec 10 '15 at 22:12
  • 3
    obligatory "this should be the accepted answer". Another addition though; if you maybe know the name of the window from the second argument of window.open, you can use this: `window.open('example.org', 'the-window-name')` can be checked with `if (window.opener && … window.name === 'the-window-name')` – Adrian Föder Aug 18 '17 at 13:40
  • 1
    This does not work if you close the "opener" browser window. Then `window.opener` is `null`. The same thing happens when the user is redirected to another domain ([source](https://stackoverflow.com/a/18712160/12534)). – Christian Davén Nov 07 '18 at 11:25
  • Christian Davén - Interesting find. For the part about closing the opener, maybe you could immediately store the initial value of the conditional in a variable. – Steve Nov 08 '18 at 12:40
  • 1
    This is not a silver bullet. As noted in a few of the comments, `window.opener` will return `true` **even if not a popup window** if the window was opened with `window.open` and even a few other cases. Please be aware of this. – phillyslick Mar 01 '19 at 20:27
  • It's doesn't work, if you use relative links, e.g. .... React router uses this kind of links under hood. – Mega Proger Jun 30 '20 at 15:01
20

The following worked for me when testing in Chrome, Firefox, Safari, and IE8. It worked for a window created useing window.open() or target=_blank.

if (window.opener) {
    alert('inside a pop-up window or target=_blank window');
} else if (window.top !== window.self) {
    alert('inside an iframe');
} else {
    alert('this is a top level window');
}
BarelyFitz
  • 1,959
  • 13
  • 17
4

window.locationbar.visible not necessarily only for a popup, but can help detecting if the user can change the location manually via location bar...

Community
  • 1
  • 1
Zaucy
  • 279
  • 2
  • 11
  • Much better to look at `window.menubar.visible`, as in [this related question](https://stackoverflow.com/a/14954591/12534). – Christian Davén Nov 07 '18 at 11:30
  • For a Chrome extension, I needed to distinguish a popup window from the same code loaded in an browser action popup. I assumed `menubar.visible` would be `false` in both cases, since neither kind of popup shows a menubar. But it's actually `true` in the browser action case, so that can be used to distinguish these two kinds of extension popups. – jdunning May 17 '20 at 01:35
3

I use this code to determine a popup window

(window.opener && window.opener !== window && !window.menubar.visible)
  • 1
    The `window.opener &&` bit is unnecessary, since if `opener` is `null`, it won't equal `window`. – jdunning May 17 '20 at 01:37
  • 1
    `window.opener &&` bit is necessary. Otherwise, when `window.opener` is null, it will not equal window and it will think it's a popup when it's not – Ali Saeed Mar 15 '21 at 19:45
2

The following statement will be true if window is a popup window or subframe:

 window.parent != window
Maksym Kozlenko
  • 9,961
  • 2
  • 64
  • 54
1

Why not just check if the opener is NOT undefined? a popup has an opener while a regular page hasn't.

if (window.opener != null) 
BornToCode
  • 8,657
  • 9
  • 57
  • 77
0

This is not a sure fire method, but typically a popup using window.open() does not direct to a new URL, so both windows will have the same href. Therefore, in many cases:

window.location.href == window.opener.location.href

will be true for a popup. So you could do:

var isPopup = (window.location.href == window.opener.location.href);

Like I said, this works in my limited tests, so there may be some feedback that shows where this won't be reliable.

Anthony
  • 35,330
  • 24
  • 93
  • 158
  • widnow.open() can open blank window and have the conent created with JavaScirpt or have any URL, both internal or external set (obviously you won't be able to control popup window with another doamin URL due to same domain policy). – Maksym Kozlenko Apr 20 '12 at 05:55
  • Right. If the URL is specified, this won't work. But usually when the url is specified, under a situation such as the OP's, he' would probably want whatever code injection to happen anyway. Not for sure, but more often than not. – Anthony Apr 20 '12 at 06:00
  • just to add you have a mistake if window.location.href == window.opener.location.href is true than its not popup – Amir Bar Jan 27 '16 at 19:34
  • Agree with @AmirBar, I've extended this example with: `isPopup = (window.opener!=undefined);` – Jester Oct 31 '18 at 01:23
0

The best way, is simply to check the window width, then try to resize the window width in a pixel or two, and then check if the current width equals or not to the one before the resize

in other words, if you succeeded to resize the window, you're probably inside a popup.

good luck.

jsbuster
  • 165
  • 7
0

For me, i was implementing a Logout screen that could be shown in a window popup via window.open(..) or from a link within a single page.

I needed to determine:

1) if my Logout screen is within a popup, then:

event.preventDefault();
window.close();

2) otherwise use the browser back function..

window.history.back();

My solution: If the page is within a popup, in my case it has a window page history of 1:

event.preventDefault();
if (window.history.length === 1) {
    window.close();
} else {
    window.history.back();
}
c121hains
  • 95
  • 1
  • 6
0

I was implementing 'close' button for popup window screen and 'cancel' button for '_self' window in "Typescript/Nodejs" and believe it will work for JavaScript as well. So I needed to determine if screen is opened in a popup

if(window.opener || window.history.length === 1) 
     isPopupWindow = true;
else 
   isPopupWindow = false;

"window.opener" works for almost all the browsers but having issue with IE so to handle IE issue I used "window.history.length".