Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling browser window through Javascript

I have a web app that launches an URL in other windows/tabs. I would like to check if the window/tab exists. If not, I want to create it, else I would like to pick it in the first position. I use:

wf=window.open(address, web_form_target, 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=640,height=450');
if(wf!=null)
    wf.focus();
                            

But it goes well only the first time (in IE, not in Firefox). If I create a new tab in the window, when I call window.open() nothing happens. If I close the window it recreates it but keeps it ionized.
Is there a way I can follow to obtain a good result?

Thanks in advance.

like image 888
Cris Avatar asked Nov 05 '22 08:11

Cris


1 Answers

Here's some code I've used for ages that still works as far as I know. Notice that oWindow has global scope, and that I pass it to the second parameter of open as a string, not as the object itself. Then, I test to see if it's closed before trying to open again...if it's already opened, then I just give it focus:

var oWindow;

function openWindow(p_strURL) {

    if(!oWindow || oWindow.closed) {
        oWindow = window.open(p_strURL, "oWindow", "status, scrollbars, resizable, width=800, height=500");
        if(!oWindow.opener) {
            oWindow.opener    = window;
        }
    }
    else {
        oWindow.location.href = p_strURL;
        oWindow.focus();
    }
}

Hope it helps you find a solution,

Kevin

like image 200
Kevin Nelson Avatar answered Nov 09 '22 03:11

Kevin Nelson