1

I'm maintaining a large application. In some areas, I have to check if the current window is a popup (opened using window.open()) or a new window (a new tab or window opened using target="_blank").

Here is an example of my problem:

    function CancelOutOfPage(cancelPath) {
        if (cancelPath != null && cancelPath != "" && window.opener == null) {
            location.href = cancelPath;
        } else if (referrerUrl != "" && window.opener == null) {
            // Just go back
            location.href = referrerUrl;
        } else {
            // It is a popup, close it.
            // MY PROBLEM IS HERE. IF THE WINDOW IS NOT A POPUP, BUT A AN OPENED PAGE
            // THE WHOLE WINDOW WILL CLOSE
            window.close();
        }
    }
Vin Shahrdar
  • 1,151
  • 1
  • 12
  • 30
  • Check out this [Check whether a window is Popup or not?](http://stackoverflow.com/questions/10240398/check-whether-a-window-is-popup-or-not) – Zura Sekhniashvili Jul 12 '16 at 15:10
  • @ZuraSekhniashvili I did, but if I'm understanding this correctly, we can determine if the window is "'inside a pop-up window or target=_blank window'" if the window.opener is not null. But How do I check if I'm just inside a popup window, or just inside a target=_blank? – Vin Shahrdar Jul 12 '16 at 15:13
  • @VinShahrdar Is the new `window` opened from you application? – guest271314 Jul 12 '16 at 15:18
  • @guest271314 Yes, from many different sources. – Vin Shahrdar Jul 12 '16 at 15:25

3 Answers3

1

You could just set a global variable when you open the popup, and then you know it's a popup if variable is a truthy value - if it's undefined it will be false:

In the calling page:

var popupWindow = window.open(page);
popupWindow.isPopup = true;

In the new window:

if (window.isPopup) { 
    window.close();
}

Update:

You could alternatively set the name of the window when you open with a popup. The window.open function takes a second parameter for the name, followed by optional window features.

In the calling page:

window.open("test.html", "Test Popup");

In the new window:

if (window.name.length) { 
    window.close();
}
Gideon Pyzer
  • 22,610
  • 7
  • 62
  • 68
  • The problem is that there are possibly hundreds of window opens in the entire system. I was wondering if there is a way to handle this using minimal effort. Would checking for window.name work? I might be wrong, but window.name is only set when we open a new popup. – Vin Shahrdar Jul 12 '16 at 15:30
  • @VinShahrdar Yes, that should work too. See updated answer. – Gideon Pyzer Jul 12 '16 at 15:47
0

You can give flag on window when you open it.

//in window from which you open new window
var w = window.open('');
w.someFlag = true;

//in opened window
console.log(window.someFlag);
if (window.someFlag){
    //do what you want
}
Zura Sekhniashvili
  • 1,147
  • 7
  • 19
0

You can set the name of the new window by providing second parameter to window.open(), e.g.;

 var w = window.open("url", "__blank__")

at opened window,

 if (this.name === "__blank__") {/*do stuff*/} else {/*do other stuff*/}
guest271314
  • 1
  • 15
  • 104
  • 177