0

I'm opening a popup window using

window.open(url, title, 'width=1055,height=750,scrollbar=yes');

When clicking something on that popup, I want to close the popup and then redirect the browser tab that opened the popup. I achieve this by doing

window.opener.location.href = 'newurl.php';
window.close();

I'm now adding some validation to check whether the browser tab that opened the popup is still available or if it's been closed. Now I'm doing this:

if(window.opener) {
    window.opener.location.href = 'newurl.php';
    window.close();
} else {
    window.location.href = 'newurl.php';
}

What I want to do now is on the else portion, instead of using the popup window, I'd like to open a new tab on the main browser window. I hope I was clear on what I wanted help with. Let me know if you need more information.

Note

I am aware that using a modal is probably a better approach but this decision is not for me to make.

dokgu
  • 4,957
  • 3
  • 39
  • 77
  • Handle directly the close of the opener tab instead of testing it from the popup – AlexBay Jan 08 '16 at 15:19
  • If I recall correctly, there is no way to choose between opening a tab vs a new window in Javascript, it is not exposed to the API. I would love to learn if that isn't the case though. Edit: Confirmed my assumption. http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript – Rob Foley Jan 08 '16 at 15:21
  • @AlexBay and @Rob, I think I didn't make my question clear enough. I just fixed it now by using `window.open('newurl.php', '_blank');` – dokgu Jan 08 '16 at 17:35

1 Answers1

1

After some trial and errors I finally got it to work. Here's the changes I made:

if(window.opener) {
    window.opener.location.href = 'newurl.php';
} else {
    window.open('newurl.php', '_blank');
}

window.close();
dokgu
  • 4,957
  • 3
  • 39
  • 77