Lahiru Madhusanka
Lahiru Madhusanka

Reputation: 270

How to check window.open() opening same page twice

I want to open a page with javascript window.open() and i want to check if that page is opened in a window before just bring it to front and if that page not opened before open it with a new window.

I am making a music player and i want to add it to pages in my site. when a user click "open player" button it will open the player popup window. but it was opened by the user before when button clicked it would be bring that opened window rather than opening a new window.

Any javascript or jquery solutions are there?

This is the function i using to open new window.

function popUp() {
        window.open("popup_player.php", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400",true);
    }

Upvotes: 0

Views: 2308

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074295

If you give the window a name (the second argument), on the second call window.open will just focus the window rather than opening a new one. So:

window.open(yourUrl, "aWindowName");

The browser may reload the window on the second open. If you don't want that, you can use a blank URL:

var wnd = window.open("", "aWindowName");
if (!wnd.location.hostname) {
    wnd.location = yourUrl;
}

Upvotes: 1

Related Questions