1

I am opening multiple windows with window.open, on click. I want to know, 10 seconds later, which window is still open. Is there a way to uniquely identify a window I just open?

I use the code below, but if I open a window, close it, then open another one, the code will throw the alert "window still open" twice. I understand why, but is there an easy way to identify the window, so I can check var_win.closed for the window I want, and not for the one open last?

<div class="get_link" data-url="http://www.yahoo.com">Window 1</div>
<div class="get_link" data-url="http://www.yahoo.com">Window 2</div>
<div class="get_link" data-url="http://www.google.com">Window 3</div>
<div class="get_link" data-url="http://www.space.com">Window 4</div>        

Javascript:

jQuery(document).ready(function(e) {
e( "body" ).on( "click", ".get_link", function(t) {
    t.preventDefault();

    var n = e(this);
    var u = n.data("url");

    var_win=window.open(u);

    function checkwindow(){
        if (!var_win.closed)
                alert( "window still open");
        else 
                alert ("window closed");
    }
    setTimeout(checkwindow, 10000);
    return false
})
})
ntalbs
  • 28,700
  • 8
  • 66
  • 83
MikeC
  • 25
  • 2

1 Answers1

0

No you cannot. If I understand correctly, you want to detect if a user say for example goes to Yahoo! or Facebook (a site separate from your own) and if they decide to close that window, then do something or get some information out of it.

Perhaps if the links are going to sites you own you could add some logic on those specific sites to send requests when they are being closed with an eventListener unload (although even this isn't 100% reliable) but if they are sites outside of your control, then there isn't much way for you to get that information. There would be some serious security issues if you could detect someone's interaction on other pages from your own site.

A hacky alternative if say you were showing these sites in a modal is (I guess) you can show an iFrame in a modal of the site the user would be navigating to. This wouldn't be a new tab or even a new window in their browser -- rather this would be a modal you create in your site so you have total control. This wouldn't be just a simple window.open though and would require much more work on your end.

Community
  • 1
  • 1
aug
  • 11,138
  • 9
  • 72
  • 93
  • Too bad :(, but thanks for your reply. I's more for advertising purpose. If a user clicks on an advertisement, I want to know that window stays open for a certain amount of time. – MikeC Jan 21 '15 at 13:26
  • @MikeC Ah I understand. Yeah if you really want, you can go the alternative route with the iframe. If you do decide to go that route, feel free to post a question if you have trouble. – aug Jan 21 '15 at 19:36