37

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,878
  • 6
  • 29
  • 37

11 Answers11

40

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
  • 1,159
  • 10
  • 14
  • 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
  • 3
    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
  • This doesn't work in Safari, I am seeing `window.opener === window` when in a popup. – rcbevans Jul 25 '22 at 20:23
  • 2
    Doesn't work if opener is closed before the check because it is set null when parent is closed. – Brobic Vripiat Nov 26 '22 at 18:55
  • @BrobicVripiat Try caching the value in the popup window immediately when the popup is opened. `window.BrobicVripiatPopup = window.opener && window.opener !== window;` – Steve Dec 08 '22 at 15:06
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,967
  • 13
  • 17
5

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
  • 299
  • 3
  • 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)
2

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

 window.parent != window
Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55
2

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

  • `|| window.history.length === 1` will cause this to be true on any newly opened window, regardless if it's a poopup or not – aljgom Jun 16 '22 at 12:42
2
if (window.menubar.visible) 
    // Not Popup
else
    // Popup

window.opener is true when a window is a popup but only if the opener window is still open, if it has been closed it is false, so it won't always work for determining if a window is a popup

aljgom
  • 7,879
  • 3
  • 33
  • 28
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
  • 9,495
  • 9
  • 66
  • 83
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
-1

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
  • 36,459
  • 25
  • 97
  • 163
  • 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
-1

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
  • 173
  • 7