Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Closing Multiple Browser Windows

Posted on 2006-11-23
11
Medium Priority
?
928 Views
Last Modified: 2008-01-09
Hopefully someone may have an idea here.

We have a C# ASP.NET Application, this app can spawn many child windows from one main window, anything up to 5 levels. What we would really like is if someone clicks on logout on the main window, it knows what children are open and closes these down automatically and logs out on the main window, likewise if the session times out, the next time someone clicks on the app (on any level) it will close down all child windows and logout on the main window. I hope this makes some sort of sense and any ideas would be appreciated.

Thanks
0
Comment
Question by:coesy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
11 Comments
 
LVL 7

Expert Comment

by:rangasuman
ID: 18002761
You can achieve this using Javascript.
You would have to keep a reference in the parent window of any child windows opened to achieve this.

<INPUT TYPE=button onClick="child1=window.open ('http://www.google.com','child1')" VALUE="Open">
<INPUT TYPE=button onClick="child2=window.open ('http://www.yahoo.com','child2')" VALUE="Open">
<INPUT TYPE=button onClick="child1.close();child2.close()" VALUE="Close All">

You can add as many as windows you would like with a different reference.
0
 
LVL 4

Author Comment

by:coesy
ID: 18002796
This is ok but with our app any combination of windows could be open and we don't neccessarily know which ones are being used at the time of logout or session end.
0
 
LVL 7

Expert Comment

by:rangasuman
ID: 18003042
I did not understand what do you mean by that. If you dont know how many windows you would like to open, then you need to initialize an array for the reference and then you can loop to close the windows. If you want to selectively close only some windows, you can still do so.
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 4

Author Comment

by:coesy
ID: 18003228
Users open windows by clicking links i don't force any windows to open so i don't know which ones are open, its basically a web app that works like a windows app, so multiple forms could be open at once, its a good idea about the array though, but i am not sure how best to store this between forms, would it be better client side or server side? would you use cookies or something? also how would you know whether a form has been closed by the user or not in order to remove it from the array, i know about the onunload function but this works on both refresh and close? sorry just trying to get it clear in my head.
0
 
LVL 7

Expert Comment

by:rangasuman
ID: 18005420
Since you would like to close all the windows, you can simply use the code snippet written above. If you call a window to close and if does not exist, it should not matter. If you initialise an array, it will be on the client side with in the Javascript code and will be valid till some one closes the main window containing the code. There is no need to store this value in a cookie or any where else. There is no way we can know if a user closes a window and we would know about it. So, I suggest you to not get into array trouble unless you are good at Javascript.
0
 
LVL 4

Author Comment

by:coesy
ID: 18006381
ok, thats fine. so if i keep the array on the main screen via client-side and i add to it a window that pops up from the main screen, what happens if i click on the child 1 window to pop up a child 2 window, will the main screen now know it has a child of a child open?
0
 
LVL 7

Expert Comment

by:rangasuman
ID: 18008716
No that is not quite possible. I ll get back to you with a probable solution, if my test runs successfully.
0
 
LVL 16

Accepted Solution

by:
LeeKowalkowski earned 2000 total points
ID: 18235550
Too late for my solution?  Here's a demo:  

anyWindow.html:
    <ul>
      <li>
        <a href="javascript:open('anyWindow.html');void 0;">Open a window using javascript</a>
      <li>
        <a href="anyWindow.html" target="_blank">Open a window using HTML target attribute</a>
      <li>
        <a href="anyWindow.html">Standard navigation, hold shift to open in a new window, or use context menu.</a>
      <li>
        <a href="javascript:logout();void 0;">logout</a>
        <p>
          Note: The logout link doesn't have to be on every page, and ideally loads a page which contains and calls the logout function as near as possible to the top of the head of the document.
        <p>
          The HREF of the logout link just calls the logout function immediately for demonstration purposes, in a real application, you'll probably want to do some server-side processing first, so just navigate to a logged out page containing the logout script.
    </ul>
    <script>
      var loggedOut = false;

      var isChildWindow = opener && opener.logout;

      if(isChildWindow)
      {
        // check for logout condition 4 times/second.
        setInterval("closeWindowWhenLoggedOut()", 250);
      }

      function closeWindowWhenLoggedOut()
      {
        try
        {
          loggedOut = opener.loggedOut;
        }
        catch(e) // cannot access opener.loggedOut if parent window has been closed.
        {
          loggedOut = true;
        }

        if(loggedOut)
        {
          // close the window.
          opener = null;
          close();
        }
      }

      function logout()
      {
        if(isChildWindow)
        {
          opener.logout();
        }
        else
        {
          goToLogoutPage();
        }
      }

      function goToLogoutPage()
      {
        location.assign("logout.html");
      }
    </script>

logout.html:
    <script>loggedOut = true;</script>
    <p>Thank you for using our application.</p>

- Should work a treat!  Although I don't know what kind of application you're writing, there may be some adverse behaviour, for example:

If the main window opens window-2 which in turn opens window-3 - closing window-2 will also close window-3 - but that might make sense.


So how do you get it to work?  Just put the SCRIPT block in a .js file and include it on every authenticated/application page, pop-ups and main window.  Then the "loggedOut = true" script on the page served when the user has logged out.


Thanks, any problems let me know...  
0
 
LVL 4

Author Comment

by:coesy
ID: 18235914
talk about pulling it out the hat! thats fantastic, just tried it on our application and it done the job perfectly, thanks very much, really appreciate it
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question