26

I have a html page. In the body of the page I am calling onload event which calls javascript function to open a pop up window. here is the code:

var newWindow = null;
function launchApplication()
{
    if ((newWindow == null) || (newWindow.closed))
    {
        newWindow = window.open('abc.html','','height=960px,width=940px');
    }
}

when I move to another page, and come back to that page again, popup reopens, although it is already opened. Please guide me to proper direction so that if pop up is already open then it should not open again. I tried document.referred but it requires the site online, currently I am working offline.

Code Spark
  • 178
  • 14
Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
  • that is because when you move to another page the newWindow reference is not accessible any more. When you move back to the page the newWindow is initialised to null and you get a new popup. I am afraid there is nothing you can do using javascript or any client side technology. Any one with a different idea? – MaVRoSCy Aug 27 '12 at 08:08
  • with a server side technology this might be possible – MaVRoSCy Aug 27 '12 at 08:09
  • 1
    @MaVRoSCy how would a server do what's clearly a matter of the client? If there's a server-side tech that does it, then we need to file security reports and work to shut it down. – Jon Hanna Aug 27 '12 at 08:11
  • should write cookie or put the value in session?? – Saghir A. Khatri Aug 27 '12 at 08:36
  • @Jon Hanna yes this is possible, using session variables to know if the popup is open. Using ajax you can check that session variable and when returning using a javascript function you can choose to open or not the popup based on the return of the AJAX call – MaVRoSCy Aug 27 '12 at 08:50
  • @MaVRoSCy AJAX is client-side. – Jon Hanna Aug 27 '12 at 08:54
  • @Jon Hanna and session management is clearly server side :P – MaVRoSCy Aug 27 '12 at 08:57
  • @MaVRoSCy yes, but you were saying it couldn't be done on the client and then suggest a way that you can do it "server side" that involves the client. It wouldn't work anyway as it won't know if the window was closed, nor obtain a reference to the window if it is open (they may want to open a different resource or use `newWindow` in some way). They're much better of just doing it the normal client-side way. – Jon Hanna Aug 27 '12 at 09:00
  • i was searching it thoroughly, so far concluded that it aint possible!!! without involving server.. and still there will be problem, if the window is closed, thn i have no way to knw, either it is closed or not, so that to reopen window again – Saghir A. Khatri Aug 27 '12 at 09:08
  • @Jon Hanna you can use window.onunload to notify the server of any change in the popup state isn't it? – MaVRoSCy Aug 27 '12 at 09:08
  • @SaghirA.Khatri what problem did you have with my answer? – Jon Hanna Aug 27 '12 at 09:09

5 Answers5

22
newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px');

Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.

Never make up a name that begins with _, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).

Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.

Edit:

Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title will affect that, as of course will code in abc.html). It does affect other attempts to do stuff across windows. Hence another window.open with the same name will reuse this window. Also a link like <a href="def.html" target="com_MyDomain_myWindowForThisPurpose">clicky!</a> will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
14

To open a window and keep a reference to it between page refresh.

var winref = window.open('', 'MyWindowName', '');
if(winref.location.href === 'about:blank'){
    winref.location.href = 'http://example.com';
}

or in function format

function openOnce(url, target){
    // open a blank "target" window
    // or get the reference to the existing "target" window
    var winref = window.open('', target, '');

    // if the "target" window was just opened, change its url
    if(winref.location.href === 'about:blank'){
        winref.location.href = url;
    }
    return winref;
}
openOnce('http://example.com', 'MyWindowName');
Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
8

You can check if the window is open or closed by re-assigning a reference to it when it closes. Example:

var newWindow;
var openWindow = function(){
    newWindow = newWindow || window.open('newpage.html');
    newWindow.focus();
    newWindow.onbeforeunload = function(){
        newWindow = null;
    };
};
Andy Taw
  • 2,794
  • 1
  • 17
  • 9
1

Use the "closed" property: if a window has been closed its closed property will be true. https://developer.mozilla.org/en-US/docs/Web/API/Window/closed

0

When you move on another page (on the same domain), you can re-set the window.open variable with popup page like this :

https://jsfiddle.net/u5w9v4gf/

Step to try :

  1. Click on Run (on jsfiddle editor).
  2. Click on Try me (on preview).
  3. Click on Run to move on another page, the variable will be re-set.

Code :

window.currentChild = false;

$("#tryme").click(function() {
    if (currentChild) currentChild.close();
    const child = window.open("about:blank", "lmao", 'width=250,height=300');
    currentChild = child;

    //Scrope script in child windows
    child.frames.eval(`
    setInterval(function () {
        if (!window.opener.currentChild)
            window.opener.currentChild = window;
    }, 500);
  `);
});


setInterval(function() {
console.log(currentChild)
    if (!currentChild || (currentChild && currentChild.closed))
        $("p").text("No popup/child. :(")
    else
        $("p").text("Child detected !")
}, 500);
user2226755
  • 12,494
  • 5
  • 50
  • 73