2014-02-19 108 views
0

在我的應用程序中,當會話過期時,我將應用程序重定向到某個session_expiry.jsp,因此在重定向到session_expiry.jsp之前,如果有彈出窗口打開然後我需要關閉使用JavaScript。我無法使用彈出式名稱,因爲我無法硬編碼整個應用程序的名稱。如何在javascript中一次關閉所有打開的彈出窗口

請幫助我,如果你有任何想法。

感謝, Jampanna

+0

[關閉所有彈出窗口(可能重複http://stackoverflow.com/questions/17756376/close-all-pop-up-windows ) –

+0

複製[獲取打開的彈出窗口列表](http://stackoverflow.com/questions/9212537/get-list-of-opened-popup-windows) – jfriend00

回答

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(); 
+0

感謝您的回覆,其實我不想硬編碼任何名稱,這裏我必須在'instanceName'中提及它是一個彈出式名稱或其他,請澄清我的疑問 – Jampanna

+0

您不必傳遞彈出式名稱..其用於打開並關閉..但你只需要關閉所有功能即可。關閉功能不需要彈出的名稱。 –

+0

它不工作!我得到null或不是WindowDialog變量的對象。 – Jampanna

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> 
+0

你有沒有? –

相關問題