adsense

Thursday, January 22, 2015

Asp.net Closing child windows from JavaScript

These maybe situations where we use some of the additional information displayed  using window.open() method. It can cause issue if we keep these open widows when dealing with different user logins within the system.

The solution is to store all opened windows in an Javascript array and close them on logout or navigation to other links or menu options.

 var openWindows = new Array();

function OpenWindow(url){
          
            openWindows.push(window.open(url));
        }


We can use the window.onbeforeunload event to close the opened windows as shown below.

   window.onbeforeunload = function () {
            closeWindows();
        };


 function closeWindows() {
            for (i = 0; i < openWindows .length; i++) if (openWindows [i] && !openWindows[i].closed)
                openWindows[i].close();
        }

Cheers
Samitha

No comments:

Post a Comment