English 中文(简体)
How do I make sure that an automatically closed window closes all its spawned windows as well?
原标题:

I have the following scenario:

  1. A user on one our customer s website clicks a link. It performs an automatic signon with their credentials to our site, which is launched in a new window.
  2. User on our site logs out, and is sent to a page which does a JavaScript window.close(), which closes the window. If they were a customer that logged into our site directly then they would go back to the Login page, but being automatically signed on, they have no use for the Login page, so the page automatically closing is better.

However sometimes the following happens

  1. A user on one our customer s website clicks a link, performs an automatic signon with their credentials to our site, which launches in a new window.
  2. While in our application they click on the "Help" link, which at this time is another popup window.
  3. At some point they log out of our app, which closes that window.
  4. The help window is still open.
  5. The user clicks on a link in the help window.
  6. The user, not being logged in anymore, now goes to a login screen in that help window.
  7. MASS HYSTERIA!

So what I d like to do is ensure that when that original window is closed, it also closes any popup windows it spawns. I figure this can be done in JavaScript but how?

ADDENDUM: The suggestions below are nice but the problem is the page the user is on when the browser closes is not the same page that spawns the popup, so the handles to the pages are all gone. How can I get the handle to a popup window with a specific name?

最佳回答
<html>
<head>
<script>
var childwindows=new Array();
function openchild(){
    var childW=window.open( in.html );
    childwindows[childwindows.length]=childW;
}

function closewindow(){
    for(i in childwindows){
        if(childwindows[i] && !childwindows[i].closed)  {childwindows[i].closewindow();}
    }
    window.close();
}
</script>
</head>
<body>
<br/>
<a onclick="openchild();">open child window</a>
<br/><br/>
<a onclick="closewindow();">close me</a>
</body>
</html>
问题回答

Keep track of the window names you open the popups with.

Have a page that purely does a window.close().

When you would do a normal window.close(), make sure you do a window.open("our close page", windowNameIStored) for each of the popup windows.

This is a fix I submitted downstream to a vendor of ours and works quite well.

Stop using pop-ups, this isn t 1998 :)

Seriously, though with all of the built-in browser functionality and 3rd party apps which also block them, they ve become pretty much unreliable. Are you 100% sure the pop-up opened? You ll have to test to make sure, but then maybe a 3rd party app closed it after your test finished, who knows, so now the customer is having a bad experience. If they re actually paying customers, your client could be losing money. Sure, you can put a message which says "turn off your pop-up blocker", but honestly how many users actually know how to do that?

Every time I m asked to do a pop-up, I just stare at whoever requested it like they are speaking some other language, then ask if it is 1998. Pop-ups are the result of one or more of the following: bad User Interface design, ignorant managers, and/or sloppy/lazy development. Though I m sure there are other reasons.

Just my 2-cents.





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签