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 w
The following statement will be true if window is a popup window or subframe:
window.parent != window
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');
}
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();
}
I use this code to determine a popup window
(window.opener && window.opener !== window && !window.menubar.visible)