0

In my application I am redirecting the application to some session_expiry.jsp when session has expired, so in that moment before redirecting to session_expiry.jsp if any popups were opened then I need to close using javascript. I can't use the popup names because I can't hard code the names for whole application.

please help me if you have any idea.

Thanks, Jampanna

Jampanna
  • 21
  • 5
  • possible duplicate of [Close all pop-up windows](http://stackoverflow.com/questions/17756376/close-all-pop-up-windows) – Standin.Wolf Feb 19 '14 at 05:54
  • Duplicate of [Get list of opened popup windows](http://stackoverflow.com/questions/9212537/get-list-of-opened-popup-windows) – jfriend00 Feb 19 '14 at 06:03

2 Answers2

0
var WindowDialog = new function() {
this.openedWindows = {};

this.open = function(instanceName) {
    var handle = window.open(Array.prototype.splice.call(arguments, 1));

    this.openedWindows[instanceName] = handle;

    return handle;
};

this.close = function(instanceName) {
    if(this.openedWindows[instanceName])
        this.openedWindows[instanceName].close();
};

this.closeAll = function() {
    for(var dialog in this.openedWindows)
        this.openedWindows[dialog].close();
};
};


// close all dialogs

WindowDialog.closeAll();
  • Thanks for your response, actually I don't want to hard code any name , here what I have to mention in 'instanceName' is it a popup name or other, please clarify my doubt – Jampanna Feb 19 '14 at 06:40
  • you dont have to pass popup name.. its for to open and close.. but you need only closeall function thats it. And closeall function not required popup name. – Mehul Rajput Feb 19 '14 at 07:07
  • It's not working!! I am getting null or not an object for WindowDialog variable. – Jampanna Feb 19 '14 at 07:30
  • I used this code like...var WindowDialog = new function() { this.openedWindows = {}; this.closeAll = function() { for(var dialog in this.openedWindows) this.openedWindows[dialog].close(); }; }; WindowDialog.closeAll(); is it correct? by this code am getting null or not an object script error at WindowDialog, can you please send correct way of using this code to close all the popups – Jampanna Feb 19 '14 at 08:27
  • tell me how you open pop ? – Mehul Rajput Feb 19 '14 at 08:38
  • popWin = window.open("something.do? "AdmissionDisplayPS", "width=350,height=440,scrollbars=1,resizable=1,status=1,top=80,left=100"); – Jampanna Feb 19 '14 at 08:56
  • please take an array and store all popups when you open it. like document.MyActiveWindows= new Array; function openWindow(sUrl,sName,sProps){ document.MyActiveWindows.push(window.open(sUrl,sName,sProps)) } function closeAllWindows(){ for(var i = 0;i < document.MyActiveWindows.length; i++) document.MyActiveWindows[i].close() } – Mehul Rajput Feb 19 '14 at 09:39
0
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">

var windows = [];
function conf() {
    var rand_no_c = Math.random();
    var con = rand_no_c.toString();
    var_win = 'child'+con.substring(2);
    windows[var_win] = window.open('child.aspx',var_win,'width=1100,height=700,left=50,top=20,status=0');
}

function closewindow() {
for(w in windows) {
    if(!windows[w].closed) {
        windows[w].close();
        }
    }
}
</script>

<style type="text/css">
    * {margin:0;padding:0;}
</style>

</head>
<body>
<div>
    <button type="button" onclick="conf();">open child window</button>
    <button type="button" onclick="closewindow();">close all windows</button>
</div>

</body>
</html>