0

I have a website that opens a new window. I am trying to trigger onclose event on the child window (if the user closed the window the parent window will alert it).

I found a stackoverflow question regarding that problem at: How to run function of parent window when child window closes?

But, the answer also preforms action on the child window which I think I can't do because the child window opens a website not in my control (I can't edit its html).

I was thinking of using the following to trigger the on close event

var inFormOrLink;
$('a').live('click', function() { inFormOrLink = true; });
$('form').bind('submit', function() { inFormOrLink = true; });

$(window).bind("beforeunload", function() { 
    return inFormOrLink;
})

How do I apply this on the new tab/window?

Community
  • 1
  • 1
Neta Meta
  • 4,001
  • 9
  • 42
  • 67
  • I've found something that could have been helpful but again they are preforming action on the child window. http://stackoverflow.com/questions/1777864/how-to-run-function-of-parent-window-when-child-window-closes – Neta Meta Sep 12 '12 at 03:04
  • Do you have control over the original site that the child window loads? – Ian Sep 12 '12 at 03:13
  • Yes i do have control over the parent(original website) i do not have control over the child ones though if there is a way of adding the functions to the child windows then i could some how do it. – Neta Meta Sep 12 '12 at 03:47

2 Answers2

0

This is not possible to do solely on the client-side. In order to do this, you'll need to do the following:

  • Upon opening a tab, send an ajax call to a script that doesn't stop loading until it receives a second ajax call, this is usually done with a script that waits for an sql value to exist before outputting.
  • Upon closing the tab, send the second ajax call to that script so that it replies to the original ajax call.

Viola.

I'm not aware of a simpler way to do this.

Original window:

function waitForTabClose(windowID) {
    $.post('waitfortabclose.php',{windowID:windowID},function(data) {
        if (data == 1) { 
             // Do tab closed stuff
        } else {
            waitForTabClose(windowID);
        }
    });
}

waitfortabclose.php

$i=1;
$wID = $_POST['windowID'];
do {
    if (file_get_contents($wID) == $wID) {
        echo 1;exit;
    }
    set_time_limit(30);
    sleep(5);
} while($i++ < 50);
echo 0;

New Tab

window.onclose = function() {
    $.post('windowclosed.php',{windowID:windowID);
};

windowclosed.php

$wID = $_POST['windowID'];
file_put_contents($wID,$wID);

This is pseudocode, and hasn't been tested. The functionality can be written in a lot of ways, this is just how I'd do it.

Korvin Szanto
  • 4,531
  • 4
  • 19
  • 49
  • wouldnt that requires me to have control over the second pop up window as well(sever side) ? the pop up window usually loads other websites which means i am unable to change anything server side there. – Neta Meta Sep 12 '12 at 02:00
  • Then to be perfectly honest, I don't think it's possible. You need some way of communicating between windows through the server, if not there isn't any way of you doing this. – AlbertEngelB Sep 12 '12 at 04:46
  • Korin thanks for all your help and other but solution were found look at my answer – Neta Meta Sep 12 '12 at 05:05
0

Found a solution to what i were looking for. it was much easier then the above answer plus its actually works and what i were looking for.

var win = open('http://www.google.com');
if (win.closed) {
    alert('Window closed! Hoorah!');
}

Thanks very much for whoever tried helping.

pbaris
  • 4,525
  • 5
  • 37
  • 61
Neta Meta
  • 4,001
  • 9
  • 42
  • 67